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(); } } |
chevron_right
filter_none
(A) Derived::show() called
(B) Base::show() called
Answer: (A)
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.
Quiz of this Question
Recommended Posts:
- Java | Inheritance | Question 9
- Java | Inheritance | Question 2
- Java | Inheritance | Question 3
- Java | Inheritance | Question 4
- Java | Inheritance | Question 5
- Java | Inheritance | Question 7
- Java | Inheritance | Question 8
- Java | Inheritance | Question 9
- Inheritance in Java
- Java and Multiple Inheritance
- Inheritance and constructors in Java
- Delegation vs Inheritance in Java
- Comparison of Inheritance in C++ and Java
- Interfaces and Inheritance in Java
- Using final with Inheritance in Java



