Java Inheritance

Question 1
Output of following Java Program?
class Base {
    public void show() {
       System.out.println("Base::show() called");
    }
}
 
class Derived extends Base {
    public void show() {
       System.out.println("Derived::show() called");
    }
}
 
public class Main {
    public static void main(String[] args) {
        Base b = new Derived();;
        b.show();
    }
}
Tick
Derived::show() called
Cross
Base::show() called


Question 1-Explanation: 
In the above program, b is a reference of Base type and refers to an abject of Derived class. In Java, functions are virtual by default. So the run time polymorphism happens and derived fun() is called.
Question 2
class Base {
    final public void show() {
       System.out.println("Base::show() called");
    }
}
 
class Derived extends Base {
    public void show() {
       System.out.println("Derived::show() called");
    }
}
 
class Main {
    public static void main(String[] args) {
        Base b = new Derived();;
        b.show();
    }
}
Cross
Base::show() called
Cross
Derived::show() called
Tick
Compiler Error
Cross
Runtime Error


Question 2-Explanation: 
Final methods cannot be overridden. See the compiler error here.
Question 3

Java

class Base {
    public static void show() {
       System.out.println("Base::show() called");
    }
}
 
class Derived extends Base {
    public static void show() {
       System.out.println("Derived::show() called");
    }
}
 
class Main {
    public static void main(String[] args) {
        Base b = new Derived();
        b.show();
    }
}
Tick
Base::show() called
Cross
Derived::show() called
Cross
Compiler Error


Question 3-Explanation: 
Like C++, when a function is static, runtime polymorphism doesn't happen.
Question 4
Which of the following is true about inheritance in Java?

1) Private methods are final.
2) Protected members are accessible within a package and 
   inherited classes outside the package.
3) Protected methods are final.
4) We cannot override private methods. 
Tick
1, 2 and 4
Cross
Only 1 and 2
Cross
1, 2 and 3
Cross
2, 3 and 4


Question 5
Output of following Java program?
class Base {
    public void Print() {
        System.out.println("Base");
    }         
}

class Derived extends Base {    
    public void Print() {
        System.out.println("Derived");
    }
}

class Main{
    public static void DoPrint( Base o ) {
        o.Print();   
    }
    public static void main(String[] args) {
        Base x = new Base();
        Base y = new Derived();
        Derived z = new Derived();
        DoPrint(x);
        DoPrint(y);
        DoPrint(z);
    }
}
Tick
Base
Derived
Derived
Cross
Base
Base
Derived
Cross
Base
Derived
Base
Cross
Compiler Error


Question 5-Explanation: 
Question 6
Predict the output of following program. Note that fun() is public in base and private in derived.
class Base {
    public void foo() { System.out.println("Base"); }
}
 
class Derived extends Base {
    private void foo() { System.out.println("Derived"); } 
}
 
public class Main {
    public static void main(String args[]) {
        Base b = new Derived();
        b.foo();
    }
} 
Cross
Base
Cross
Derived
Tick
Compiler Error
Cross
Runtime Error


Question 6-Explanation: 
It is compiler error to give more restrictive access to a derived class function which overrides a base class function.
Question 7
Which of the following is true about inheritance in Java. 1) In Java all classes inherit from the Object class directly or indirectly. The Object class is root of all classes. 2) Multiple inheritance is not allowed in Java. 3) Unlike C++, there is nothing like type of inheritance in Java where we can specify whether the inheritance is protected, public or private.
Tick
1, 2 and 3
Cross
1 and 2
Cross
2 and 3
Cross
1 and 3


Question 7-Explanation: 
Question 8
Predict the output of following Java Program
// filename Main.java
class Grandparent {
    public void Print() {
        System.out.println("Grandparent's Print()");
    }
}
 
class Parent extends Grandparent {
    public void Print() {
        System.out.println("Parent's Print()");
    }
}
 
class Child extends Parent {
    public void Print() {
        super.super.Print(); 
        System.out.println("Child's Print()");
    }
}
 
public class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.Print();
    }
}
Tick
Compiler Error in super.super.Print()
Cross
Grandparent's Print()
Parent's Print()
Child's Print()
Cross
Runtime Error


Question 8-Explanation: 
In Java, it is not allowed to do super.super. We can only access Grandparent's members using Parent. For example, the following program works fine.
// Guess the output
// filename Main.java
class Grandparent {
    public void Print() {
        System.out.println("Grandparent's Print()");
    }
}
 
class Parent extends Grandparent {
    public void Print() {
    	super.Print();  
        System.out.println("Parent's Print()");
    }
}
 
class Child extends Parent {
    public void Print() {
        super.Print();  
        System.out.println("Child's Print()");
    }
}
 
class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.Print();
    }
}
Question 9
final class Complex {

    private final double re;
    private final double im;

    public Complex(double re, double im) {
        this.re = re;
        this.im = im;
    }

    public String toString() {
        return "(" + re + " + " + im + "i)";
    }
}

class Main {
  public static void main(String args[])
  {
       Complex c = new Complex(10, 15);
       System.out.println("Complex number is " + c);
  }         
}
Tick
Complex number is (10.0 + 15.0i)
Cross
Compiler Error
Cross
Complex number is SOME_GARBAGE
Cross
Complex number is Complex@8e2fb5
Here 8e2fb5 is hash code of c


There are 9 questions to complete.

  • Last Updated : 27 Sep, 2023

Similar Reads