Q4 What is the output of the following code 10 points public

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\";
}
}

Solution

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

Q4. What is the output of the following code: (10 points) public class InheritanceTester { public static void main(String[] args) { MySubClass mysub = new MySub

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site