• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Confused with protected access

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am confused as to how protected members are accessed. In the KB book, it says that protected members are accessed only through inheritance. So if you try to access the member of a superclass in a subclass in a different package using the instance of the parent class then javac will through an error.

Then what about this code



Appreciate your help,
Kits
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi kitty,

From your code, you already extended the ModifierTest. With that alone, the protected variable x is already visible int the Test class. You cannot access a protected member thru instantiating the superclass.Take note, inheritance is different from instantiation. You can only access protected members of the superclass thru inheritance.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's your question Kitty? That code doesn't compile, and you've provided the explanation why.
 
Kitty Dayal
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies.

The above code compiles and runs fine.

One of the Dan's mock exam question,


Answer: d,e.

Why is there no compile error in c. Isn't the protected member m2() accessed by creating an instance and not by inheritance? I am confused as to how the protected members can be accessed.

Thanks,
kits
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the protected access modifier allows access to all classes within the same package and also all sub-classes(even if they are in different packages)

So if I have a class(B) in the same package as that of the protected member(in class A), then I can actually create an instance of class A in class B and use the protected member. Not exactly inheritance, since B is not a sub-class of A.

So protected access modifier is not "only" through inheritance. Outside the package--YES, but not within the same package.
 
arnel nicolas
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why is there no compile error in c. Isn't the protected member m2() accessed by creating an instance and not by inheritance? I am confused as to how the protected members can be accessed.


That code above will compile fine because C already inherited the public and protected properties of A. So it is legal to call method m2().

The first code you have mentioned is different from the 2nd one because from there you instantiated your superclass ModifierTest in your Test class (this is not inheritance) and tries to access the protected member, definitely it will not compile. The Dan's mock exam will compile fine because the superclass A was not instantiated...it was inherited and then it creates an instance of C in order to call the method m2() inherited from A. You need to instantiate C because you are in a static method (main method).

On the other hand, i do agree with the above explanation of Murtuza.
[ August 13, 2004: Message edited by: arnel nicolas ]
 
Kitty Dayal
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi arnel,

Thanks everyone for their replies. I now understand it clearly.

Kits
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic