Inheritance


  1. What is the output of this program?

    class N
    {
    int K;
    void display()
    {
    System.out.println(K);
    }
    }
    class M extends N
    {
    int L;
    void display()
    {
    System.out.println(L);
    }
    }
    public class inheritance_Example
    {
    public static void main(String args[])
    {
    M object = new M();
    object.K=5;
    object.L=6;
    object.display();
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    Class N & class M both contain display() method, class M inherits class N, when display() method is called by object of class M, display() method of class M is executed rather than that of Class N.


  1. Which two classes use the Shape class correctly?

    A. public class Circle implements Shape
    {
    private int radius;
    }

    B. public abstract class Circle extends Shape
    {
    private int radius;
    }

    C. public class Circle extends Shape
    {
    private int radius;
    public void draw();
    }

    D. public abstract class Circle implements Shape
    {
    private int radius;
    public void draw();
    }

    E. public class Circle extends Shape
    {
    private int radius;
    public void draw()
    {
    /* code here */
    }
    }

    F. public abstract class Circle implements Shape
    {
    private int radius;
    public void draw()
    {
    /* code here */
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    If one is extending any class, then they should use extends keyword not implements.



  1. What is the output of this program?

    class N
    {
    int K;
    int L;
    N()
    {
    K = 3;
    L = 5;
    }
    }
    public class Result
    {
    public static void main(String args[])
    {
    N obj1 = new N();
    N obj2 = new N();
    System.out.print(obj1.equals(obj2));
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    obj1 and obj2 are two different objects. equals() is a method of Object class, Since Object class is superclass of every class it is available to every object.
    output:


  1. What is the output of this program?

    final class n
    {
    int K;
    }
    class n1 extends n
    {
    int L;
    System.out.println(L + " " + K);
    }
    public class inheritance_Example
    {
    public static void main(String args[])
    {
    n1 obj = new n1();
    obj.display();
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    class n has been declared final hence it cannot be inherited by any other class. Hence class B does not have member i, giving compilation error.

    Output: Compilation Error



  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. View Hint View Answer Discuss in Forum

    NA

    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.