File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Need help with Overriding 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 » Java » Beginning Java
Bookmark "Need help with Overriding" Watch "Need help with Overriding" New topic
Author

Need help with Overriding

Kacee Saxena
Ranch Hand

Joined: Mar 04, 2008
Posts: 54
I have a query related to method overriding. As per my knowledge, overriding method in derived class can not restrict the access level of the overridden method in base class. So in below example,



final keyword should not be allowed in class Trying. Since final restricts the access and changing behavior of the method. However the code is working fine, so can anyone please help me understanding this.

Thanks
Arjan Kleene
Greenhorn

Joined: Mar 07, 2009
Posts: 4
Kacee Saxena wrote:I have a query related to method overriding. As per my knowledge, overriding method in derived class can not restrict the access level of the overridden method in base class. So in below example,



final keyword should not be allowed in class Trying. Since final restricts the access and changing behavior of the method. However the code is working fine, so can anyone please help me understanding this.

Thanks
Final is not an access level keyword. There are four access levels: public, protected, default and private. Default access level does not have a keyword.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Kacee,

The final keyword does not restrict access. It only prevents the method from being overridden. So, if you create a subclass of Trying, you won't be able to create an overriding here() method.

John.
Kacee Saxena
Ranch Hand

Joined: Mar 04, 2008
Posts: 54
Thanks for the response

I was under impression that although final is not an access level keyword but somehow it puts a limitation on the method that it cant be overridden further. I think this is a kind of "restriction" being laid on the overridden method.
I still believe that the here() method's behavior has been restricted by adding "final" keyword since the concept behind overriding is to allow a method exhibit multiple behavior in child classes.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Kacee:

The final keyword just tells the compiler to not allow the method to be overridden. It does not change any programmatic behavior. If you have an object of that class, you can still use that method (assuming that the access level allows that). Method behavior and compiler behavior are two different things.

John.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with Overriding
 
Similar Threads
Static methods & Overriding!
Upcasting
about constructors
Private methods' inheritance
Static methods and instance variables are not overridden