• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inheritance

 
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Looky! I'm being abducted by space aliens! Me and this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic