Output of Java program | Set 25 (Polymorphism)

08 Jul, 2017
Pre-requisite: Polymorphism in java
1) What is the output of the following program?
class GFG{    protected void getData()    {        System.out.println("Inside GFG");    }}class GeeksforGeeks extends GFG{    protected void getData()    {        System.out.println("Inside GeeksforGeeks");    }}  public class Test{    public static void main(String[] args)    {        GFG obj = new GeeksforGeeks();        obj.getData();    }}
a) Inside GFGb) Inside GeeksforGeeksc) Compilation errord) Runtime error
Ans. (b)Explanation: A reference variable of GFG class is used to point to an object of class GeeksforGeeks. At the time of compilation, the JVM checks whether the method being called is defined in GFG class, but at the runtime, JVM invoke the method of GeeksforGeeks class because the object is from class GeeksforGeeks. Refer to static vs dynamic binding in java for more details.
2) What is the output of the following program?
class Test{    void myMethod()    {        System.out.println("GeeksforGeeks");    }}public class Derived extends Test{    void myMethod()    {        System.out.println("GFG");    }          public static void main(String[] args)    {        Derived object = new Test();        object.myMethod();    }}
a) GeeksforGeeksb) GFGc) Compilation errord) Runtime error
Ans. (c)Explanation: A child class cannot be used as a reference to an object of super class.
3) What is the output of the following program?
class GFG{    protected void getData()    {        System.out.println("Inside GFG");    }}class GeeksforGeeks extends GFG{    protected void getData()    {        System.out.println("Inside GeeksforGeeks");    }          protected void getValue()    {        System.out.println("GeeksforGeeks");    }}  public class Test{    public static void main(String[] args)    {        GFG obj = new GeeksforGeeks();        obj.getValue();    }}
a) Compilation errorb) Runtime errorc) GeeksforGeeksd) None of these
Ans. (a)Explanation: A GFG reference variable is used to store GeeksforGeeks object. At compile time, JVM looks for getValue method in GFG class. Detecting the absence of it, JVM throws a compile time error. Refer to static vs dynamic binding in java for more details.
4) What is the output of the following program?
interface GFG{    void myMethod();    void getInfo();}  abstract class Geeks implements GFG{    void getData()    {        System.out.println("GFG");    }}  public class Test extends Geeks{    public void myMethod()    {        System.out.println("GeeksforGeeks");    }    public void getInfo()    {        System.out.println("Geeks");    }          public static void main(String[] args)    {        Geeks obj = new Test();        obj.getInfo();    }}
a) Geeksb) Compilation errorc) Runtime errord) None of these
Ans. (a)Explanation: Class Geeks implements GFG interface. So all the methods declared in GFG interface get replicated for Geeks class. So when getInfo method is called on line 1 in the program above, the compiler checks to see if getInfo method exists in the Geeks class as a variable of type Geeks is being used to reference Test object.
5) What is the output of the following program?
class Test{    public void gfg()    {        System.out.println("GeeksforGeeks");    }}public class Derived extends Test{    public void gfg()    {        System.out.println("GFG");    }    public static void main(String[] args)    {        Derived obj = new Test();        obj.gfg();    }}
a) Compilation errorb) Runtime errorc) GFGd) GeeksforGeeks
Ans. (a)Explanation: A child class reference variable cannot be used to store an instance of parent class.
This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Java-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.