This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding protected method 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 "Overriding protected method" Watch "Overriding protected method" New topic
Author

Overriding protected method

Saurabh Verma
Ranch Hand

Joined: Aug 13, 2007
Posts: 38
Hi,

Its written in K&B book,page number 102:
A subclass in a different pacakage can override only those non-final methods marked public or protected.


But when I write the code:



Getting compile error at commented line.
Can anybody explain why I am getting error.
Thnks.
Raphael Kou
Greenhorn

Joined: Oct 23, 2007
Posts: 13
Because you defined your Base Typ "Vehical" at this line

Vehical car = new Car();

The compiler is looking for the start method in the Vehical class (at runtime the start method in the car class will called), but because you marked it protected you have no access to the start method.
[ October 24, 2007: Message edited by: Dzu-I Kou ]
Saurabh Verma
Ranch Hand

Joined: Aug 13, 2007
Posts: 38
Hi,Dzu-I Kou Thnks for the reply.
As I am overriding start() method.Could called that method through the refernce of super class.
Raphael Kou
Greenhorn

Joined: Oct 23, 2007
Posts: 13
I hope I got your question.

In the Car class, you cannot access Vehical methods through a Vehical Object (except the method is public). You can use the super keyword to access Vehicals methods (if override) or just direct.
[ October 24, 2007: Message edited by: Dzu-I Kou ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding protected method
 
Similar Threads
Protected Member Access Problem
protected constructor
Protected acces override
why compile time error???
doubt in IS-A relationship