Open In App

Output of Java program | Set 25 (Polymorphism)

Last Updated : 08 Jul, 2017
Improve
Improve
Like Article
Like
Save
Share
Report

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 GFG
b) Inside GeeksforGeeks
c) Compilation error
d) 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) GeeksforGeeks
b) GFG
c) Compilation error
d) 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 error
b) Runtime error
c) GeeksforGeeks
d) 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) Geeks
b) Compilation error
c) Runtime error
d) 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 error
b) Runtime error
c) GFG
d) GeeksforGeeks

Ans. (a)
Explanation: A child class reference variable cannot be used to store an instance of parent class.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads