• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubts about Inheritance  RSS feed

 
Andre Brito
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm from Brazil. First of all, sorry my English.
I'm studying Head First Java, of Kathy Sierra and Bert Bates.
So, chapter 7 (I guess) is about Heritage (superclass and subclasses) and here's the quote:


Private members are not inherited
Public members are inherited


But, take look at my 2 codes.

First one, the superclass:


Now, the Subclass:


The result is:


s.getPrivate() = 100
s.getPublic() = 0
s.setPrivate(s.getPublic);
s.setPublic(s.getPrivate);
s.getPrivate() = 0
s.getPublic() = 100


So, I accessed the private member and changed it.
Why did Kathy Sierra and Bert Bates said that it's not possible to access the private members?

It must be something that I'm not understanding very well or the translation for portuguese. It's not the first time that I see this on a book that was translated for portuguese :/

Cumps from Brazil!

[ edited title to be more descriptive - Jim ]
[ January 07, 2008: Message edited by: Jim Yingst ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what you can't do is this:

s.privateMember = 12;

but you can do this:

s.publicMember = 8

you can't DIRECTLY access the variable, but you can access the methods. Since your subclass IS-A superclass, it has to behave like one, so the variables have to be there somewhere.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All members (also known as instance variables) are inherited.

Private instance variables are "out of scope", they are hidden from all subclass implementations (and everyone else).

They are still there.
If they have explicit accessors (as in your example), they can be meaningfully used.

It's also possible that you couldn't meaningfully, directly access or modify the variables, but they might change as a side effect of other code.
 
Andre Brito
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm...
First of all, thank you for the answers.

So, if it's like 1 class, right? The private, I can't access, and the public it's ok, right?

I can't do this (like Fred said): s.privateMember = 1000, but all the public members I can access AND modify. Is that right? And I can manipulate my private members, since I have a method to do that. right?

Thanks folks, and cumps from Brazil!

BTW - Thanks Jim! I don't know what you did, but thanks anyway
[ January 07, 2008: Message edited by: Andre Brito Fonseca ]
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andre Brito Fonseca:
...
I can't do this (like Fred said): s.privateMember = 1000, but all the public members I can access AND modify. Is that right? And I can manipulate my private members, since I have a method to do that. right?



Yes, You are right


BTW - Thanks Jim! I don't know what you did, but thanks anyway


He mentioned what he did at the bottom of your very first post. It says he edited the subject to be more descriptive

Have a nice time here
 
Andre Brito
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raghavan Muthu:


He mentioned what he did at the bottom of your very first post. It says he edited the subject to be more descriptive

Have a nice time here


Oh! Hehehe... thanks Muthu!
Cumps!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!