Java | Inheritance | Question 9
Predict the output of following program. Note that foo() 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(); } } |
chevron_right
filter_none
(A) Base
(B) Derived
(C) Compiler Error
(D) Runtime Error
Answer: (C)
Explanation: It is compiler error to give more restrictive access to a derived class function which overrides a base class function.
Quiz of this Question
Recommended Posts:
- Java | Inheritance | Question 4
- Java | Inheritance | Question 5
- Java | Inheritance | Question 1
- Java | Inheritance | Question 3
- Java | Inheritance | Question 8
- Java | Inheritance | Question 2
- Java | Inheritance | Question 7
- Java | Inheritance | Question 9
- Inheritance in Java
- Java and Multiple Inheritance
- Interfaces and Inheritance in Java
- Using final with Inheritance in Java
- Comparison of Inheritance in C++ and Java
- Inheritance and constructors in Java
- Delegation vs Inheritance in Java
Improved By : Ashish Kumar Yadav



