Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

private/public and polymorphism

Thread view: 
Samuel Topwash - 19 Mar 2006 14:17 GMT
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 -----
Alan Krueger - 19 Mar 2006 17:48 GMT
> 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."


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.