• 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

Is Protected Method is accessible?

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package testpkg.p1;
public class ParentUtil
{
public int x = 420;
protected int dostuff()
{
return x;
}
}



package testpkg.p2;
import testing.p1.ParentUtil;
public class ChildUtil extends ParentUtil
{
public static void main(String[] agrs)
{
new ChildUtil.callStuff();
}

void callStuff()
{
System.out.println("this"+this.dostuff());
ParentUtil p = new ParenUtil();
System.out.println("parent"+p.dostuff());//Ans: Error In this line
}
}

Can any one tell why it will show error? Can�t we access the protected member or method from which class extends the other class which is in different package?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kiran,

When u call this.doStuff() u are calling the inherited protected member.Since the ChildUtil class is a subclass of ParentUtil class the method is inherited.

But in case of calling doStuff() with the parent class reference, u can access only the public members.
Protected members are accessbile only within the same package and in their subclasses even in different package) only.

Check this link to know more about accessbility.
http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

Hope this helps.

Brindha.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember, protected access modifier always refers to <package + kids >.
that means this modifier is accessible to subclass outside package only thru inheritence.

It is illegal to make an instance of superclass and access protected member outside package as protected member would act as private or default to this instance.
Also, protected member is inherited by derived class outside package and not by any other subclass of the derived class.
it becomes private in the subclass outside package.

Please concentrate on rules of protected modifier outside package
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Kiran Chand ,

here is the example to understand the concept

package testpkg.p2;
import testing.p1.ParentUtil;
public class ChildUtil extends ParentUtil
{
public static void main(String[] agrs)
{
ChildUtil child= new ChildUtil();
ParentUtil parent= new ParentUtil();

child.protectedMehtod();....we can do it
parent.protectedField; ...possible
parent.protectedMethod();///ERROR
parent.protectedField; //ERROR
}

}

this means through child class reference can have access to protected members but not through parent class reference. This is the reason why we use protected keyword
 
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic