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

Protected members

Ajay Xavier
Ranch Hand

Joined: Jan 03, 2005
Posts: 109
HI,

i am having three java files in two different packages.

package one;

public class One
{
protected int i=10;
protected void method1(){}
}

package two;
public class Two extends One
{
public void method2()
{
method1(); // accessed thru inheritance bcoz base class and subclass are in different packages
}

}

package two;
public class Three extends Two
{
public void method3()
{
Three tree=new Three();
tree.method1(); //shouldn't compile bcoz it tries to access base class protected members.
}
public static void main(String args[])
{
Three tree=new Three();
tree.method3();
}
}

In the above example class Three shouldn't compile bcoz its available in different package(two) and accessing protected member of the class One available in the another package(one). But is compiling fine and works well .once a class(Two) outstide the pacakage(two) inherits a class(One) in different package(one) the protected members of the base class(One) becomes private to the subclass(Two) . but in this case class which extends that subclass is able to access the protected members of the base class is it Correct?

i think the Enacapsulation policy is violated?

can any one explain me ?
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
protected : Is available to all classes in the same package and also available to all subclasses of the class that owns the protected feature.This access is provided even to subclasses that reside in a different package from the class that owns the protected feature.

You must be thinking of default, available only to classes in same package.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 988
    
    1
If you want a variable to be available only to the containing class and other classes in the same package, use the "package" modifier.

From "Java in a Nutshell"


(...which, by the way, I keep taped to my bookshelf, because I always forget how package and protected work.)

Ryan
Ajay Xavier
Ranch Hand

Joined: Jan 03, 2005
Posts: 109
Originally posted by Carol Enderlin:
protected : Is available to all classes in the same package and also available to all subclasses of the class that owns the protected feature.This access is provided even to subclasses that reside in a different package from the class that owns the protected feature.

You must be thinking of default, available only to classes in same package.i think when a subclass inherits protected members from the base class it becomes private to the subclass and no code outside the subclass can access it .
but in this case class (Two) is inheriting the protected members of the base class (One) . so the protected members of the base class(One)becomes private members to this subclass(Two). now any class which extends this subclass (Two) can't access this protected members.but it is possible

i dont know why it is ?
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
Inherited methods and fields do not automatically become private, in fact they can have any access that is smaller or equal in scope to the fields and methods of the super class.

If the field/method is public in the parent class, it can be public, protected, "package", or private in the child class.

If it is protected, it can be protected, "package", or private.

If "package", then (if accessable) it can be "package" or private.
Edwin Keeton
Ranch Hand

Joined: Jul 10, 2002
Posts: 214

i think when a subclass inherits protected members from the base class it becomes private to the subclass and no code outside the subclass can access it .


Not true. A descendant class inherits all of the protected, package and public members of all of its ancestor classes, now matter how deep the inheritance tree. To paraphrase what Timmy said, an inherited protected member does not become private unless the descendant class specifically declares it private. So Class3 inherits method2 from Class2, and i and method1 from Class1.

You may be confusing this with the visibility of inherited protected members in a descendant class to other classes in its own package. Look at this example:


SCJP, SCWCD
Ajay Xavier
Ranch Hand

Joined: Jan 03, 2005
Posts: 109
Originally posted by Edwin Keeton:


Thanks for ur explanation

i got it now
Hilary Mann
Greenhorn

Joined: Jun 07, 2012
Posts: 13
Ajay Xavier wrote:
Originally posted by Edwin Keeton:


Thanks for your explanation

i got it now


But, it does behave like private, doesn't it?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Chances are the original posters won’t be back in a hurry.compile the three classes
What behaves as private? Your question is not at all clear. If you mean the protected method of class 1, no it does not behave as a private member. Try changing the access of that method in class 1 to private, and then re‑compile the three classes. You will see different behaviour: failure to compile. If you try to execute the app, you will find the old versions of the .class files there, and you may suffer exceptions because they are the wrong type or version.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Protected members
 
Similar Threads
Access Modifiers and Package 's
confusion of protected
Protected members inheritance
question about member variable
Protected and default access of a class member