Page 1 of 1

Compilation error

Posted: Sat Feb 24, 2018 2:22 am
by Meghana
What will be printed when the following code is compiled and run?

class A
public int getCode(){ return 2;}
}

class AA extends A {
public long getCode(){ return 3;}
}

public class TestClass {

public static void main(String[] args) throws Exception {
A a = new A();
A aa = new AA();
System.out.println(a.getCode()+" "+aa.getCode());
}

public int getCode() {
return 1;
}
}
The code doesn't compile because the methods, "getCodes()" are not defined in a static context and we are trying to access them from a static method is it?

Re: Compilation error

Posted: Sat Feb 24, 2018 2:29 am
by Meghana
Also, when is the ClassNotFoundException thrown?
(When I tried to run the above code, this exception was thrown)

Re: Compilation error

Posted: Sat Feb 24, 2018 4:56 am
by admin
In your first post you are saying the code doesn't compile but in your second message you are saying you got CNFE when you ran the code? So I am not sure what exactly are you doing. Did the code compile or not? What is the error message displayed by the compiler if it did not compile?

Please post the exact code that you are trying to compile.

Re: Compilation error

Posted: Sat Feb 24, 2018 5:39 am
by Meghana
admin wrote:In your first post you are saying the code doesn't compile but in your second message you are saying you got CNFE when you ran the code? So I am not sure what exactly are you doing. Did the code compile or not? What is the error message displayed by the compiler if it did not compile?

Please post the exact code that you are trying to compile.
1. The correct answer to the question was: It does not compile. I was not sure as to why it did not compile.
2. Also, later when I tried to run the code as it is, I got the ClassNotFoundException. I wanted to know what I was doing wrong. (I guess its a basic question but I haven't practically worked on java much)

Re: Compilation error

Posted: Sat Feb 24, 2018 5:40 am
by Meghana
This is the question with the code:

Quote:
What will be printed when the following code is compiled and run?

class A
public int getCode(){ return 2;}
}

class AA extends A {
public long getCode(){ return 3;}
}

public class TestClass {

public static void main(String[] args) throws Exception {
A a = new A();
A aa = new AA();
System.out.println(a.getCode()+" "+aa.getCode());
}

public int getCode() {
return 1;
}
}

Re: Compilation error

Posted: Sat Feb 24, 2018 5:42 am
by Meghana
To be precise,
1. I don't know as to why the right answer is: It does not compile.
2. When is a CNFE thrown?

Re: Compilation error

Posted: Sat Feb 24, 2018 11:32 am
by admin
It seems like you are using an IDE to run the code because it is not possible to run a java file directly from command line. You have to compile it first. IDEs sometimes hide the compilation step and that is why it is not recommended to use Ides for exam preparation.

So try to compile the given code from command like and read the error message carefully.

CNFE is thrown by the jvm when you try to load a class but the jvm is not able to find it.

Re: Compilation error

Posted: Sat Feb 24, 2018 11:38 am
by Meghana
I'll try doing that from now. Using Eclipse for now.
Thank you so much! :)