• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritence  RSS feed

 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a little confused regarding the exact meaning of the word "inheritence". If private members are not inherited, then how can they be accessed after doing a cast to the superclass?



By "inherited", I understand that "is contained within". So it seems that everything that a superclass object has must be present in the subclass object (since, afterall, a subclass object IS-A superclass object), and the access modifiers control what is "accessible" , rather than what is "inheirted". (Essentially, they are "access" modifiers, not "inheritence" modifiers.

This doubt is the result of learning that "in Java private members are not inherited".
[edited]
[ July 27, 2006: Message edited by: Neelesh Bodas ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When someone says "private members are not inherited", they mean "direct access to those members is not inherited, either for the inheriting class or for other classes." The members are indeed there, and you can't get at them directly, but the base class part of the object can still work with them -- as your program clearly demonstrates.

I personally can't stand this now very standard way of describing things, because, as you can see, it's very confusing without further explanation. But there's not much I personally can do about it

By the way, the cast is completely unnecessary -- because B extends A, you can call "foo()" directly on b.
[ July 27, 2006: Message edited by: Ernest Friedman-Hill ]
 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation. Got the point.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!