Hi, This is a code from Bruce Eckel's Thinking in Java public class PrivateOverride { private void f() { System.out.println("private f()"); } public static void main(String args[]) { PrivateOverride po = new Derived(); po.f(); } } class Derived extends PrivateOverride { public void f() { System.out.println("public f()"); } } 2 question on this... 1.Can u create an object of child class in the parent class itself 2.Why is the o/p "private f()" even though po is an object of type Derived. Please help !!
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
0
1) yes 2) because private methods are inlined at compile time and therefore do not participate in polymorphism
Can we look at it other way saying that "private methods of base class are not available (not seen) in the derived class in order to be overridden?? Please clarify
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
0
Yes.
Anthony Villanueva
Ranch Hand
Joined: Mar 22, 2002
Posts: 1055
posted
0
A private method can't be overriden nohow.
8.4.6.1 Overriding (by Instance Methods) An instance method m1 declared in a class C overrides another method with the same signature, m2, declared in class A if both 1. C is a subclass of A. 2. Either m2 is non-private and accessible from C, or m1 overrides a method m3, m3 distinct from m1, m3 distinct from m2, such that m3 overrides m2.