Method Overriding In Java? Simple Ways to Overload Methods

croe java topics

What Is Method Overriding In Java?

Method Overriding In Java? It is the process of providing the subclass specific method implementation and Logic for the inherited method. If the subclass provides the particular implementation of any method, which is declared by its superclass, is known as method overriding in java.

When the method is inherited in the superclass to subclass.

then in the subclass, we can change the Method Logic by keeping the same declaration as declared in the superclass. 

Method overriding is possible only in the case of Inheritance.

Representation of Overriding:-

A Real Example of an overriding program in java?

Java Program of Overriding Example 1:-

Java Program of Overriding Example 2:


SoftwareEngineer works
CivilEngineer works

Method overriding rules in java?

  • The method must be inherited in the superclass to the subclass.
  • The return type of method must be the same as declared in the superclass. 
  • The method name must be the same as declared in the superclass.
  • Method name, context type, or a signature must be the same as declared as in the superclass, but Logic can be different. 
  • Access modifications must be the same as declared in the superclass or can be of higher visibility.
  • We can optionally use annotation (@Override) called an override (annotation is introduced from JDK 1.5)

Read More: Top Ways to do Method Overriding In Java?

Usage of Java Method Overriding?

  • Method overriding is used to implement a specific implementation of a method that is already provided by its superclass. 
  • Method overriding is applied to achieve Runtime polymorphism in java.

Can we override final method in java?

No, we can notIf we don’t want a method to be overridden, we declare it as final.

Can the main method be overridden?

No, we can not override the main method, because it is static, but We can Overload the main method.

Constructor overriding in java?

No Constructors can not be overridden. When you try to create a constructor, you will get a compile-time error in your program.

Suppose Parent class and child class has the same methods with signature, return type, and parameters. And when you try to call constructor using the child class object, then the child class method is invoked. 

Constructors are looks like methods, but both are different. Because constructors do not have a return type, their names are the same as those of a class name.

Example of Overriding with different signature?


In the case of Method Overriding, the access modifier can be the same as declared in the superclass or can be of higher visibility.

  • The private method can not be Override, because private Methods can not be inherited. 
  • The final method can be inherited, but can not be overridden. 
  • The purpose of method overriding is to achieve Runtime Polymorphism

Also Read: What is Java, Full Introduction of Java for Beginners with Examples?

Difference between Method overloading and overriding in java?

S.noMethod OverloadingMethod overriding
1Method Overloading is used to increase the readability of the program.Method overriding is used to provide the particular implementation of the method that is provided by its class.
2Method overloading is performed within the class.Method overriding occurs in two classes that have an Is-A relationship (inheritance).
3In the case of method overloading, the parameter must be different.In the case of method overriding, the parameter must be the same.
4Method overloading is an example of compile-time polymorphism.Method overriding is an example of runtime polymorphism.
5In java, method overloading can’t be performed by changing the return type of the method only. Type can be the same or different in method overloading. But you must have to change the parameters.The return type must be the same or covariant in method overriding.

method overloading and method overriding in java


What is override definition?

the simple definition of override is to providing the subclass specific method implementation and Logic for the inherited method.

Define override Example in java?

You can get examples of override after the first paragraph.

What are the restrictions, when overriding a method?

If You want to know about the restrictions, read the rules for overriding.

Admin Words: If you have any Question regarding the method overriding you can ask in the comment box.

0 0 votes
Article Rating
croe java topics

Previous articleData Types in java – Primitive and Non-Primitive Data Types
Next articleConstructor in Java, Rules, Types, and Ways to Create Constructor | 2021
Notify of
Inline Feedbacks
View all comments