DEV Community

Pavithraarunachalam
Pavithraarunachalam

Posted on

Method Overriding in Java..

What is method overriding?

Method Overriding is a subclass(child class) to provide a specific implementation of a method that is already defined in its superclass(parent class).

Why use method overriding?

To change the behavior of a method in a subclass without modifying the superclass. It helps achieve runtime polymorphism (deciding which method to call during program execution).

Rules of Method Overriding in Java:

  - The method name must be the same.

  - The parameters (number and type) must be the same.

  - The return type must be the same (or covariant).

  -  The method in the parent class must not be:

              1. private (not accessible)

              2. final (cannot be changed)

              3. static (belongs to class not object)

              4. You can use @Override to help catch errors.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)