File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Protected acces override Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Protected acces override" Watch "Protected acces override" New topic
Author

Protected acces override

Juan Tamayo
Greenhorn

Joined: Mar 18, 2007
Posts: 7
What happens when a subclass outside the package of the superclass overrides a protected method of it's superclass? According to what I see in the following code, the method becomes visible to all classes in the same package as the subclass.



The code compiles and runs OK. Then the question is, am I not breaking the whole purpose of "protected"?
Louis Moloney
Ranch Hand

Joined: Feb 06, 2007
Posts: 59
you dont even need to override the goFast() method to get the same behaviour, as you have inherited the method.

in answer to your question maybe your right in way.

however if you have a protected method (inherited or not), it must be visible to all classes in the same package for consistancy.


Juan Tamayo
Greenhorn

Joined: Mar 18, 2007
Posts: 7
The problem here is that unless I override the method in the Legacy class, the Outback class will not have access to the goFast() method in Legacy
[ March 18, 2007: Message edited by: Juan Tamayo ]
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
The protected member is only visible outside the package through inheritance.

Another classes outside the package cannot access the protected member directly. For these classes the protected member is to considerd private.

You're defining a new method which has the same signature in the subclass outside the package.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Amit Wadhwaa
Ranch Hand

Joined: Feb 15, 2007
Posts: 74
According to the Outback class its accessing the method of Legacy not Car, Now its upto the implementor of Legacy class (who has been given the privilege to extend Class Car by the author of that Class) to implement the method. If the Legacy class programmer has the option of giving access to any protected member(incl. vars/methods) of CAR Class to other classes through its method. Its a implementation choice of Legacy Class Programmer.The Car class author knew that the method could be accessed in such a way and still kept the method protected so he didn't want to guard from a implementation like this.


SCJP 5 94%<br /><a href="http://amit-wadhwa.blogspot.com/" target="_blank" rel="nofollow">My Blog</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Protected acces override