This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Visiblity: Default vs. protected access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Visiblity: Default vs. protected access" Watch "Visiblity: Default vs. protected access" New topic
Author

Visiblity: Default vs. protected access

Erich Tiegelholz
Greenhorn

Joined: May 27, 2012
Posts: 10
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

Joined: Jan 28, 2008
Posts: 5575

when you use javac what error you are getting?
Erich Tiegelholz
Greenhorn

Joined: May 27, 2012
Posts: 10
Seetharaman Venkatasamy wrote:when you use javac what error you are getting?


Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Don't try to run uncompilable code. Post the compiler error.


luck, db
There are no new questions, but there may be new answers.
Erich Tiegelholz
Greenhorn

Joined: May 27, 2012
Posts: 10
Sorry for that. Here is the output of javac ./test/Car.java.

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: May 27, 2012
Posts: 10
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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: May 27, 2012
Posts: 10
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

Joined: May 27, 2012
Posts: 10
For anyone else who reads this in the future, I adjusted some comments (class AccessTest):

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
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

Joined: May 27, 2012
Posts: 10
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

Joined: Oct 13, 2005
Posts: 37884
    
  22
Erich Tiegelholz wrote:Thanks for your welcome!
You’re welcome
This is not about any real project. . . .
Thank goodness for that!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Visiblity: Default vs. protected access
 
Similar Threads
Cloning
protected variable access
Cannot acces Protected Variable
protected access
Protected acces override