Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is the output of the following code: public class InheritanceTester { publi

ID: 3923190 • Letter: W

Question

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

      }

}

Explanation / Answer

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote