Home » JAVA Programming » Inheritance » Question
  1. What is the output of this program?

    class n
    {
    int K;
    public void display()
    {
    System.out.println(K);
    }
    }
    class m extends n
    {
    int L;
    public void display()
    {
    System.out.println(L);
    }
    }
    public class Dynamic_dispatch_Example
    {
    public static void main(String args[])
    {
    m obj2 = new m();
    obj2.K = 3;
    obj2.L = 5;
    n r;
    r = obj2;
    r.display();
    }
    }
    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
Correct Option: E

r is reference of type n, the program assigns a reference of object obj2 to r and uses that reference to call function display() of class m.



Your comments will be displayed only after manual approval.