All IT Courses 50% Off
JAVA Tutorials

Method overriding in Java

Introduction to Method Overriding:

  • Firstly, let’s understand the overriding in object-oriented terms. Overriding means override the functionality of an existing method.
  • If the subclass or child class has the same method as we declared in the superclass or parent class, it is called Method overriding in Java.

Use of Method Overriding:

  • It is used to give us the implementation of a method that is already provided by its superclass.
  • It is used for runtime polymorphism (many forms).

Rules about the Method Overriding:

  • First, the method signature, like method name, its parameter list, and also the return type, have to match exactly.
  • The method should have the same name as same as the parent class.
  • The method must have the same parameter as the same as the parent class has.

Method overriding

Method overriding in Java

Difference between Method Overloading and Method Overriding:

Method Overloading:

    • It is in the same class, where we have more than one method having the same name but different parameters.   

Method Overriding:

  • It is one of the methods which is also defined in the subclass as the same in the superclass
  • In this, the parameters of the method remain the same.    

Note: Method Overriding is mostly used to achieve Run Time Polymorphism.

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button