• 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

Question regarding protected modifier

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hiii
My question is that suppose there is a class A in one package and a class B which is in another package..
Class B can access the protected instance variables of A through inheritance (without an object of A )that i know..
But can class B not access the protected member functions of A through inheritance???
Please clarify i am having doubt...
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened when you tried it??
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But can class B not access the protected member functions of A through inheritance???



I dont understand this ?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A member variable or method that is protected can be accessed by the class in which it is defined itself, in classes that are in the same package as the class, and in subclasses of the class (also if such a subclass is in another package).
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It can access both the Instance variables and Methods of class A. In a nutshell, it can access the Members(Instance variables and Methods) of class A (if they are protected).
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In few words:

protected = package + kids

package = in the same package, protected behaves as public,
kids = in another package, access is possible only through inheritance.

Think of a rich man who wants to donate (his protected goods). He says:
Ok, my family (the same package) should receive. In addition, if I meet another person not belonging to my family (other package), only his kids should participate, as well (I say, only the kids - because it's unlikely that they'll disabuse my money drinking too much *lol)

Here a quick code example:



Hope this helps!
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Briefly, if class B extends class A, it can access A's protected
members even if B is in a different package.

Jim ... ...
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ujjawal rohra wrote:Hiii
My question is that suppose there is a class A in one package and a class B which is in another package..
Class B can access the protected instance variables of A through inheritance (without an object of A )that i know..
But can class B access the protected member functions of A through inheritance???
Please clarify i am having doubt...



Class B can access A's instance variables or its member functions only through inheritance! Otherwise Compilation Error!
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ujjwal,

Protected members, be it methods or variables are accessible through inheritance only, if the child class is outside the
package. For other classes, they are like, they don't even exist.

Best Regards,
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prithvi Sehgal wrote:Protected members, be it methods or variables are accessible through inheritance only, if the child class is outside the
package. For other classes, they are like, they don't even exist.



From the JavaDoc...

6.6.2 Details on protected Access

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.



Basically, in the case where a subclass wants to access the protected members of the super class, via a reference that is not the "this" reference -- and the subclass is not in the same package as the super class -- it can only do so, if it can be confirmed that the object IS-A sub class. This means that the superclass member can be accessed, if the referenced used is of a type subclass.

Henry
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent explanation Henry.

Best Regards
 
Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic