What is method overriding: When a subclass provides a specific implementation for a method that is already defined in its parent class, it is called method overriding. The overridden method in the subclass must have the same name, parameters, and return type as the method in the parent class.
Rules for method overriding:
a) Name, parameters and return type must be of same type as parent.
b) Static methods cannot be overridden.
c) At runtime, Java selects the method based on the real object in memory rather than the reference type.
d) Method overriding requires inheritance.
Static methods cannot be overridden, defining a static method in a subclass with same signature as in the super-class hides the super-class method.
Instance methods can be overridden, but a subclass cannot override a super-class static method.
A static method in a subclass with the same signature as a super-class static method hides the original method.
Private methods cannot be overridden because they are not visible to sub-classes.
A subclass method with the same name is treated as a new, independent method, unrelated to the parent class.
Top comments (0)