This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question regarding protected modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question regarding protected modifier" Watch "Question regarding protected modifier" New topic
Author

Question regarding protected modifier

ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105
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...


SCJP 6
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

What happened when you tried it??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
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).

SCJP 6.0 96%

(Connecting the Dots ....)
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
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!


SCJP 6 (88%), SCWCD (89%)
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Briefly, if class B extends class A, it can access A's protected
members even if B is in a different package.

Jim ... ...


BEE MBA PMP SCJP-6
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
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,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Excellent explanation Henry.

Best Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question regarding protected modifier
 
Similar Threads
Protected methods
protected
Confused with protected access
Protected access modifier
about package