Forums Register Login

Need help with Overriding

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
Do Re Mi Fa So La Tiny Ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 804 times.
Similar Threads
Upcasting
about constructors
Static methods and instance variables are not overridden
Static methods & Overriding!
Private methods' inheritance
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:27:36.