• 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

Protected members

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Ajay Xavier
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Edwin Keeton:



Thanks for ur explanation

i got it now
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic