Rules for overriding are :
- A method if overridden in subclass should have same argument.
- same or extended return type(Covariant)
- arguments can be declared as final additionally in subclass provided the datatype is same .
- can throw checked exception if base class method too throws the checked exception .
- the exception thrown can be either same or subclass of thrown exception in base method.
- // Rule hold only for checked exception
- Access modifiers can make it less restrictive, but not more restrictive.
A subclass cannot override fields of the superclass, but it can hide them by declaring with same name .
if want to access base class's field then user super to invoke field .
While implementing interfaces all above rules are applicable for overriding interface declared methods too.
Exception and return of covariant types are applicable .
Can a private method be overridden by a subclass?
NO! Since the subclass, cannot inherit a private method, it therefore cannot override the method—overriding depends on inheritance.
No comments:
Post a Comment