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,
Thanks for your reply!
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?
[ March 17, 2005: Message edited by: Marcos R Oliveira ]
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..."
Originally posted by Marcos R Oliveira:
... Is this true even if a class and it's subclass are not in the same package? ...
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: