The given example will test your understanding of Super class and Sub class in Java and their object initialization.
The given example will test your understanding of Super class and Sub class in Java and their object initialization.Given below the sample code :
class SuperClass {
public static void main(String[] args) {
new SubClass();
}
SuperClass() {
System.out.print("Inside SuperClass ");
}
}
class SubClass extends SuperClass {
SubClass() {
System.out.print("Inside SubClass");
}
}
What is the output of the following code ?
1. Inside SuperClass
2. Inside SubClass
3. It will give compile error
4. Inside SuperClass Inside SubClass.
(4)
Because the 'SuperClass' constructor automatically run, therefore the first output would be "Inside SuperClass" & after it, the instance of the "SubClass" is called which in turn prints "Inside SubClass".