• 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

Visiblity: Default vs. protected access

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

I use the following two source files as an example:





My question is what exactly causes the different visibility of horsepower from speed in class AccessTest?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you use javac what error you are getting?
 
Erich Tiegelholz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:when you use javac what error you are getting?



 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't try to run uncompilable code. Post the compiler error.
 
Erich Tiegelholz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for that. Here is the output of javac ./test/Car.java.

 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here c the reference variable of Convertible can access only the protected member speed since Convertible is not in the package test. Wherever you use Convertible (even in the test package), you access the class through Car's variables through Convertible and it means only protected / public variables of Car are accessible via Convertible.
 
Erich Tiegelholz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, but why isn't the same true for c.speed = 75; then? I call it from the Convertible c as well. I still don't understand why there is a difference between default and protected access inside the same package.

I totally understand that in another package, I can't do something like below even if extending Car (line 9; but could see this.speed at that position):



It's also clear to me, why speed can't be seen in any non-subclass of Car in a package different from test. Just the question above remains.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a difference in accessing the Car's class - when you access using Car's reference variable you can access both of them in the same package. But when you try to access through Convertible (which is in different package other than Car) regardless where you use it - test package or other package, you can always access only public and protected variables of Car. So you can access speed which is declared protected and not horsepower as its default.
 
Erich Tiegelholz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again, it finally got clear due to your explanations in combination with line 9 from my last example, and—in retrospect—too much head spinning. Again, thanks.
 
Erich Tiegelholz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For anyone else who reads this in the future, I adjusted some comments (class AccessTest):

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You have some design problems there. As a general rule, you should make all fields (except those used as global constants) private, and provide access to them via getXXX or setXXX methods as required. Those methods will probably have public access.
Since you probably don’t intend horsepower ever to change, you would do well to set it up in the constructor, and provide only getXXX methods for it. Make sure only to write constructors which take horsepower as a parameter.
 
Erich Tiegelholz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your welcome! This is not about any real project. I'm just learning for the SCJP exam (using the book from Kathy Sierra and Bert Bates), and the case described above was not clear to me. It is now. May be I'm slower than usual because I have a fever at present.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Erich Tiegelholz wrote:Thanks for your welcome!

You’re welcome

This is not about any real project. . . .

Thank goodness for that!
 
Did you just should on me? You should read 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