wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Protected Behaviour 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 "Protected Behaviour" Watch "Protected Behaviour" New topic
Author

Protected Behaviour

Micheal John
Ranch Hand

Joined: Nov 01, 2006
Posts: 344
Below is the quote I got from K&B 1.4 book.


I want to test it.. below is the coding...







How can ClassThree has a access to protected method in ClassOne?


Micheal John
SCJP 1.4 (86%), SCWCD 1.4 (86%), SCBCD 1.3 (85%), SCDJWS (Just Started...) - Satisfaction Lies in Our EFFORT, Not in the ATTAINMENT
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Can I suggest that you search this forum for the same topic. It has been discussed in depth not too long ago. Got no time now to search for it for you.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

It is because, the protected method becomes private to classes that try to subclass only in the same package. When you subclass in another package, then the protected member is still available.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

A protected method is accessable by any subclass, not just classes that directly inherit from it. Since ClassThree is a subclass of ClassOne (albeit not directly), it can access the protected methods.

Henry


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

Joined: Nov 01, 2006
Posts: 344
It is because, the protected method becomes private to classes that try to subclass only in the same package. When you subclass in another package, then the protected member is still available.


See the below coding where it contradicts..







All the above three classes in the same package p1. Class3 can able to access the protected method in Class1..


To Henry Wong:


A protected method is accessable by any subclass, not just classes that directly inherit from it. Since ClassThree is a subclass of ClassOne (albeit not directly), it can access the protected methods.


As per quotes, a protected method will be accessible by any sub class(which extends it) regardless of same/different packages...

When a protected method will not able available / become private? can u modify my above coding to suit it?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

As per quotes, a protected method will be accessible by any sub class(which extends it) regardless of same/different packages...

When a protected method will not able available / become private? can u modify my above coding to suit it?


As far as I know, there is no way to inherit a protected method, in a manner that will make it private. (But I have never needed such functionality)

Henry
 
 
subject: Protected Behaviour
 
Similar Threads
Protected Method
a Question about protected access midifier
protected access
Access Modifier protected -- SCJP Study Guide page 36
OO doubt