Open In App
Related Articles

Java | Inheritance | Question 9

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
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();
    }

                    
(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

Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads