Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance

 
Marcos R Oliveira
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

In Thinking in Java 3rd Edition I read this:
If you create a new package and you inherit from a class in another package, the only members you have access to are the public members of the original package.


But using JDK1.4.1 the code below compiles fine for me. Or the code and the lines above are different things?



In other file:



Shouldn�t I get an error trying to compile study/TProtect.java?

Thanks for all you time,
Marcos.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like a typo in TIJ. When you subclass another class you have (not just have access to, but have in your new class) all the public and protected members (variables and methods) of the super class. Static members are an exception, you still have access, but they are not a part of your class.
 
Marcos R Oliveira
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Stevens,

Thanks for your reply!

You said:

When you subclass another class you have (not just have access to, but have in your new class) all the public and protected members (variables and methods) of the super class. Static members are an exception, you still have access, but they are not a part of your class.


Is this true even if a class and it�s subclass are not in the same package?

Thanks again,
Marcos.
[ March 17, 2005: Message edited by: Marcos R Oliveira ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what Mr. Eckel is trying to do in that paragraph is lay the groundwork for introducing the protected keyword. But I think it would be more clear (and accurate) if he started that sentence by saying, "Using the access modifiers we've seen so far..." Then this would make sense in the context of the existing paragraph...

Using the access modifiers we've seen so far, "if you create a new package and inherit from a class in another package, the only members you have access to are the public members of the original package. ... Sometimes the creator of the base class would like to take a particular member and grant access to derived classes but not the world in general. That�s what protected does. protected also gives package access..."
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marcos R Oliveira:
... Is this true even if a class and it's subclass are not in the same package? ...

Well...

As Doug Dunn notes in Mastering the Fundamentals of the Java Programming Language, "The idea that protected fields and methods are accessible from subclasses declared in other packages is a gross oversimplification that is repeated in any number of Java books..."

Ref: http://www.javarules.com/book/sample.pdf (See section 2.9 and especially 2.10.) :roll:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic