DEV Community

Deepikandas
Deepikandas

Posted on

#11 Known is a drop! Method overriding in JAVA -part 1

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
Same Signature: The overriding method must have the exact same name, number of parameters, and type of parameters as the method in the parent class.

Inheritance: Overriding requires an "is-a" relationship (inheritance) between the parent and child classes.

Access Modifier: The access modifier of the overriding method in the subclass must be the same as, or less restrictive than (e.g., protected to public), the access modifier in the superclass.

Return Type: The return type must be the same as the parent's method, or a subtype of the parent's return type (known as a covariant return type).
Covariant return type works only with objects (reference types) not with primitive data type because it depends on inheritance.
Static/Final/Private: **static, final, and private methods cannot be overridden. Static methods are "hidden" instead.
**Exceptions:
The overriding method cannot throw new or broader checked exceptions than the method in the superclass, but it can throw narrower or fewer exceptions.

Overridden method
The superclass method being overridden is called overridden method in Java.
Overriding method
The subclass method which is overriding the superclass method is called overriding method in Java.

@override annotation

  • It standard Java annotation that was first introduced in Java 1.5. The @override annotation denotes that the child class method overrides the base class method.
    The @override annotation is useful.

  • If the annotated method does not actually override anything, the compiler issues a warning.
    It can help to make the source code more readable.

Runtime Polymorphism (Dynamic Method Dispatch)

Definition:

Happens when an overridden method is called through a parent class reference, and the method executed depends on the actual object type at runtime.

Enables flexible and reusable code.

Key Points:

Requires method overriding + upcasting.

Only instance methods can participate.

Static, final, private methods cannot be used.
Upcasting

  • Upcasting is when a subclass object is assigned to a superclass reference.
  • Always safe because a subclass is a type of its superclass.
  • Allows runtime polymorphism.
  • Limits the reference to only the methods/fields visible in the superclass, unless overridden.


Static Methods

Cannot be overridden because they belong to the class, not the object.

If a subclass defines a static method with the same signature, it hides the parentโ€™s method โ€” this is called method hiding, not overriding.

Key: Which method runs depends on the reference type, not the object type.

*Private *

  • Cannot be overridden because they are not visible to subclasses.
  • Declaring a method with the same name in the subclass is treated as a completely new method, not overriding.  Protected in parent method:


Public in parent method:


Default in Parent method:


Final is given in Parent method:

Different return type:

Top comments (0)