aspose file tools*
The moose likes Beginning Java and the fly likes Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance" Watch "Inheritance" New topic
Author

Inheritance

Marcos R Oliveira
Ranch Hand

Joined: Apr 20, 2002
Posts: 51
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.


SCJP 1.4 - SCJP 1.6 - SCWCD in progress
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
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

Joined: Apr 20, 2002
Posts: 51
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

Joined: Aug 31, 2004
Posts: 11343

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..."


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance