Q4. What is the output of the following code: (10 points) public class Inheritan
ID: 3924399 • Letter: Q
Question
Q4. What is the output of the following code: (10 points)
public class InheritanceTester {
public static void main(String[] args) {
MySubClass mysub = new MySubClass();
System.out.println(mysub.myMethod());
}
}
public class MySubClass extends MySuperClass{
public MySubClass()
{
System.out.println("Subclass constructor has been called..");
}
}
public class MySuperClass {
public MySuperClass()
{
System.out.println("Superclass constructor has been called..");
}
public String myMethod()
{
return "Superclass method has been called";
}
}
Explanation / Answer
Superclass constructor has been called..
Subclass constructor has been called..
Superclass method has been called
Explanation: Before creation of subclass object, superclass object's constructor is called. hence Superclass constructor has been called.. is before Subclass constructor has been called.. now when System.out.println(mysub.myMethod()); is executed then Superclass method has been called gets printed
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.