• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Protected method use by descendant classes.

 
Jim Crawford
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jim Crawford
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...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
Posts: 330
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jim Crawford
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic