aspose file tools*
The moose likes Beginning Java and the fly likes Polymorphism Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Polymorphism Problem" Watch "Polymorphism Problem" New topic
Author

Polymorphism Problem

Shilpa Bhargava
Ranch Hand

Joined: Sep 15, 2002
Posts: 39
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
1) yes
2) because private methods are inlined at compile time and therefore do not participate in polymorphism


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Shilpa Bhargava
Ranch Hand

Joined: Sep 15, 2002
Posts: 39
Thanks a lot Thomas ! I got it !
Jayadev Pulaparty
Ranch Hand

Joined: Mar 25, 2002
Posts: 662
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
Yes.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism Problem