Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance

 
Chitra Jay
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Khalid Mughal:
All the members of the superclass are inherited by the subclass
The answer given: true
Well,JLS says that
Constructors ,static initializers and instance initializers are not members of the class and therefore not inherited..
But what happened to private members of the superclass?? I thought that they are not inherited but visible in the subclass object.
Please Clarify
Chitra
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private members are neither inherited nor visible in the subclass.
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Mughal as well "Private members are not accessible from any other class. This also applies to subclasses, whether they are in the same package or not. It is not to be confused with inheritance of members by the subclass. ~*~ Members are still inherited, but they are not accessible in the subclass. ~*~"
So they are inherited but not accessible, what's the signeficance of that? I'm not sure, maybe it affects if a member in the subclass tries to hide the member in the super class.
Can anyone tell?
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alferd you made a very good point. To further your argument:
Let us say that a member method is inherited by sub-class because it is not private and not static. Just it happens that within this method, it access some of the private members of super class. Now you are executing this method in the sub-class as an inherited method. Technically, sub-class does not have direct access to super class private members. But via this method can access them from super class even though it is run on sub class objects.
Hope this makes sense...
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barkat,
that's exactly how private members are supposed to be accessed.
For more info about private members, please check out:
JLS 6.6.8 Example: private Fields, Methods, and Constructors
JLS 8.2.1.3 Inheritance with private
JLS 8.2 Class Members
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic