Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Visiblity: Default vs. protected access

 
Erich Tiegelholz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:when you use javac what error you are getting?


 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Sorry for that. Here is the output of javac ./test/Car.java.

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
For anyone else who reads this in the future, I adjusted some comments (class AccessTest):

 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic