aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Protected method use by descendant classes. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Protected method use by descendant classes." Watch "Protected method use by descendant classes." New topic
Author

Protected method use by descendant classes.

Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
Hi
My understanding was that a protected method (or member in general I suppose) can be used in a subclass only.
Now I have the following case:


The above seems to work... unless I'm missing something. I thought that you wouldn't be able to use 'methodX' except in 'Aplus' or other descending classes... or does the fact that you are using a descendant class provide you with the privilege to use the original base class's protected methods (or members)?
I hope I made myself clear.
Any thoughts welcome.
Regards.
[ December 11, 2003: Message edited by: Jim Crawford ]

<img src="cool.gif" border="0"> <img src="graemlins/beerchug.gif" border="0" alt="[beerchug]" /> <br />SCJP 1.4
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
I must be missing something, because if the above is correct you should be able to call 'clone()' in any class since its a subclass of 'Object'.
...investigating what the weirdness is about...

ps. this point was actually unrelated since the normal descendant rule applies.
[ December 11, 2003: Message edited by: Jim Crawford ]
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
...and it compiles...
mmm.


For that matter even this does... quite obviously.

ps. this point was actually unrelated since the normal descendant rule applies.
I suppose I got confused with abstract methods or something. i.e. that 'clone' can't be just called because it has to be implemented first... but... what's the point of declaring clone protected if it can just be called anywhere anyway since everything extends Object?

Ok, so if you don't override 'clone' then your user classes in other packages will not be able to call 'clone'. Hope I figured it out now.
[ December 11, 2003: Message edited by: Jim Crawford ]
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
An inherited protected method (or member) is also protected to its decendants. In your code:

The only reason Unrelated (not a descendant) can access methodX() is because it is in the same package as Aplus. Moving Unrelated to a seperate package will result to a compile-time error.
[ December 11, 2003: Message edited by: dennis zined ]

SCJP 1.4<br />SCWCD 1.4
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
Originally posted by dennis zined:
An inherited protected method (or member) is also protected to its decendants. In your code:

The only reason Unrelated (not a descendant) can access methodX() is because it is in the same package as Aplus. Moving Unrelated to a seperate package will result to a compile-time error.
[ December 11, 2003: Message edited by: dennis zined ]

Yes. I gathered its probably something like that. This probably caught be because of my C++ background, but its been so long since I used C++ that I can't even be sure which way it worked in C++ anyway.
Oh well... better to realize it now than in the exam.
guo mark
Ranch Hand

Joined: Oct 20, 2001
Posts: 43
The override of the abstract class made the methd "default".which made the
classes in the same package of the subclass can access the method


meet once ,love every while
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Hello guo.

The override of the abstract class made the methd "default".which made the
classes in the same package of the subclass can access the method.

Inheriting protected methods doesnt change the type of access. The reason why Unrelated was able access the method is because all of the classes were in the same package.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Protected method use by descendant classes.
 
Similar Threads
Arrays
need some help on java classes!!!!!!!!!
question from JQ+
A mock for every dependency?
Protected members