Java | Inheritance | Question 9

Predict the output of following program. Note that foo() is public in base and private in derived.

filter_none

edit
close

play_arrow

link
brightness_4
code

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


(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



My Personal Notes arrow_drop_up

Improved By : Ashish Kumar Yadav



Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.