Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question regarding protected modifier

 
ujjawal rohra
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • 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...
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried it??
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But can class B not access the protected member functions of A through inheritance???


I dont understand this ?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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).
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • 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).
 
Ulrich Vormbrock
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • 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!
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • 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 ... ...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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!
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • 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,
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Excellent explanation Henry.

Best Regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic