Java | Inheritance | 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); } } |
chevron_right
filter_none
(A)
Base Derived Derived
(B)
Base Base Derived
(C)
Base Derived Base
(D) Compiler Error
Answer: (A)
Explanation: See question 1 of https://www.geeksforgeeks.org/output-of-java-program-set-2/
Quiz of this Question
Recommended Posts:
- Java | Inheritance | Question 8
- Java | Inheritance | Question 7
- Java | Inheritance | Question 9
- Java | Inheritance | Question 9
- Java | Inheritance | Question 1
- Java | Inheritance | Question 4
- Java | Inheritance | Question 2
- Java | Inheritance | Question 3
- Inheritance in Java
- Delegation vs Inheritance in Java
- Interfaces and Inheritance in Java
- Inheritance and constructors in Java
- Java and Multiple Inheritance
- Comparison of Inheritance in C++ and Java
- Using final with Inheritance in Java