What is the output of the following code public class Inheri
What is the output of the following code:
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
Flow of execution:
1) creation of MySubClass object in main calls it\'s constructor and prints message in it
2) the MySubClass object is used to call the inherited method named myMethod which returns message in it and gets printed.
So output is :
Subclass constructor has been called..
Superclass method has been called
![What is the output of the following code: public class InheritanceTester { public static void main(String[] args) { MySubClass mysub = new MySubClass(); System. What is the output of the following code: public class InheritanceTester { public static void main(String[] args) { MySubClass mysub = new MySubClass(); System.](/WebImages/24/what-is-the-output-of-the-following-code-public-class-inheri-1060533-1761553945-0.webp)