aspose file tools*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""protected" access modifier" Watch ""protected" access modifier" New topic
Author

"protected" access modifier

Juliet Meguma
Greenhorn

Joined: Oct 07, 2004
Posts: 5
Hello!

I'm trying to understand the "protected" access modifier in Java.
My book says that a protected method/variable can be access by any class in the same package AND by any class that extends the class that contains the protected feature. That makes sense to me...

Why then does class Swan (below) NOT compile?
I thought it should compile, since it's accessing the protected variable from an instance of a class that extends the owner of the protected feature...






_______________________________________
package birdpack;

public class Bird
{
protected int nFeathers = 3;
public Bird()
{ }
}
______________________________________

package duckpack;
import birdpack.Bird;

public class Duck1 extends Bird
{
public Duck1()
{ }

public void test()
{
nFeathers = 4; // compiles ok
}
}

______________________________________
package duckpack;
import birdpack.Bird;

public class Swan extends Bird
{
public Swan()
{ }

public void test()
{
Duck1 ducky = new Duck1(); // Duck1 extends Bird
ducky.nFeathers = 4; // compile error: variable nFeathers has
// protected access in class Bird

}
}


Thanks very much!
Juliet
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
You can't "two-step" protected access. In other words, you can have one class inherit a protected method then try to have another class outside the package inherit it again. The buck stops here, so to speak, at the first inheritance level. Make sense? If not, I'll elaborate after I get my second cup of coffee!


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Juliet Meguma
Greenhorn

Joined: Oct 07, 2004
Posts: 5
Yup that makes sense... Thanks for the fast reply!
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
This is a difficult concept to grasp without going slowly.

Check out this chart:


Basically, a class has complete access to all members and methods.

a subclass has access to protected and public members.

A class in the same package has similar access privledges.

Everyone else only has access to the public menbers and methods.

Here is your Swan class:




It has extended Bird and according to the chart above, it has access to the protected variable nFeathers.

However, you have also created a new object Duck1.

Here you will only be have access to protected members of type Swan.

So while this is illegal:


This would be perfectly legal


Why? Because you are accessing a protected member of type Swan here and you are trying to access a protected member of type Duck1 in the previous statement.

Make sense? Or did I just ramble on endlessly here?

(here is an afterthought: it's overly simplified but you can think of it like this...Your swan has feathers and your swan has a buddy that happens to be a duck. Your swan can modify it's own feathers but it cannot modify the feathers of it's buddy)
[ November 08, 2004: Message edited by: jefff willis ]
Juliet Meguma
Greenhorn

Joined: Oct 07, 2004
Posts: 5
Hmmmm your explanation got me thinking...

So in this situation, it doesn't matter what classes I instantiate within the class Swan, it's the Swan class that determines what features are accessible from within that class.

So the instance of Duck1 in the Swan class must see things through Swan's eyes (right?)

I dont' know if I'm articulating it clearly but I think I'm understanding this better... You're right though, it isn't an easy concept to understand...

Thanks!
Juliet
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
Yeah, it's real easy to get lost in the terms and concepts with this.

I think you are on the right track though.

Here is some additional information that may help:

We've already established that your swan may not directly modify the ducks feathers like this:



and that the swan is free to modify it's own feathers like this:


However if the duck wanted to give permission for birds to modify it's feathers, the Duck class would have to add this method:




And then the swan could modify the ducks feathers like this:



Again, this is overly simplified. Eventually you will have to describe these same concepts in terms of "inheritance" and "encapsulation".

In more strict terms:
"A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object."

I think this is directly from Sun.
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
You think you are confused now? Just think what happens if nFeathers is protected static

Think about it..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "protected" access modifier