 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Samuel Topwash Guest
|
Posted: Sun Mar 19, 2006 2:12 pm Post subject: private/public and polymorphism |
|
|
Hi,
I have a question about the Java code below. The only difference
between the PrivateParent and the PublicParent class is that void bar() is
private in PrivateParent and public in PublicParent.
The output of the program is:
$ java Main
PrivateParent.foo()
PrivateParent.bar()
PublicParent.foo()
PublicChild.bar()
In other words, polymorphism works for the PublicParent and PublicChild
classes, but not for PrivateParent and PrivateChild. Why is this?
Is the bar() method not overridden when it is private in the parent class?
Or is there something different that I do not know about?
TIA,
Samuel Topwash,
Titaantjes b.v.
---- begin Java code -----
class PrivateParent{
public void foo(){
System.out.println( "PrivateParent.foo()" ); bar();
}
private void bar(){
System.out.println( "PrivateParent.bar()" );
}
}
class PublicParent{
public void foo(){
System.out.println( "PublicParent.foo()" );
bar();
}
public void bar(){
System.out.println( "PublicParent.bar()" );
}
}
class PrivateChild extends PrivateParent{
public void bar(){
System.out.println( "PrivateChild.bar()" );
}
}
class PublicChild extends PublicParent{
public void bar(){
System.out.println( "PublicChild.bar()" );
}
}
public class Main{
public static void main( String[] args ){
PrivateChild pc = new PrivateChild();
pc.foo();
PublicChild pc2 = new PublicChild();
pc2.foo();
}
}
---- end Java code ----- |
|
| Back to top |
|
 |
Alan Krueger Guest
|
Posted: Sun Mar 19, 2006 5:12 pm Post subject: Re: private/public and polymorphism |
|
|
Samuel Topwash wrote:
| Quote: | I have a question about the Java code below. The only difference
between the PrivateParent and the PublicParent class is that void bar() is
private in PrivateParent and public in PublicParent.
|
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.2
"Members of a class that are declared private are not inherited by
subclasses of that class." |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|