When the following java code is executed in eclipse, it gives the correct output (i.e. prints 'Class B'), but according to java specification, the code cannot be compiled (since the super class constructor requires an int
argument and the constructor provided by the compiler to class B includes a call to the super class no arg constructor, which is not defined), and when try to compile the file using javac command in command line, it only compiles the super class i.e. class A and fails with the following compile error:
B.java:8: cannot find symbol
symbol : constructor A()
location: class A
public class B extends A {
^
1 error
Following is the java file content:
class A {
public A(int x) {
//
}
}
public class B extends A {
public static void main(String[] args) {
System.out.println("Class b");
}
}
Can someone explain how eclipse executes the main method without bieng able to compile the class. Thanks in advance.
UPDATE
I deleted the .class file creatd by eclipse in the bin folder and tried to execute the class, and it could not be compiled, therefore didn't run. But when I created a source file with the public class name and saved without any content, and then added the above content, it worked. If eclipse uses the previously compiled version to execute, how could it give the output of the main
method, since it did not have the main
method at the time of success full compilation?