Method Overriding and Method Overloading in Java5 min read

Here you will learn about method overloading and method overriding with syntax and various examples and also some rules for overriding. Let us start with the method Overriding first.

Method Overriding

In any object-oriented programming language, Method Overriding is a feature that allows the user to declare a method with the same name in a sub-class that is already present in a parent-class.

That is when a sub-class inherits from its super-class and both have the same-named method than by overriding features, different tasks can be applied to both of the methods.

Method overriding is used for runtime Polymorphism.

Example of method overriding in Java:

Output method overriding:


In the above example, we can see that ‘b‘ is a type of dog but still executes the display method in the breed class. In the runtime, JVM first looks at the object type and run the method that belongs to that particular object.

Since the class Dog has the method display, the compiler will have no problem in compiling, and during runtime, the method specific for that particular object will run.

But look at the below example:


Here it shows error for the method doSleep() because the object created is of type Dog that does not have the dogSleep() method and is not Overriden in the breed class.

Rules for Overriding a Method:

  • The Overriden and Overriding methods must have the same name and the same argument list.
  • Method Overriding requires Inheritance of a class otherwise the method cannot be overridden.
  • Restriction to use Access-modifier, that is if a, access-modifier of the method is declared public in a parent class then it cannot be an overriding method of a sub-class or child-class cannot have protected, private and default access-modifier.
  • private, static and final methods cannot be overridden as they are local to the class. But the static method can be re-declared.
  • A child class within the same package as the instance’s parent class can override any parent class method that is not declared private or final.
  • Constructors cannot be overridden.
  • Overriding method (that is the method present in the sub-class) can throw unchecked exceptions, whether the Overriden method (that is the method present in the parent-class) throws an exception or not.

Use of Super keyword in Method Overriding:

The super keyword in Java is used in the sub-class for calling the inherited parent class method/constructor. When we execute super.myMethodName in sub-class inside the method, it invokes the method of parent-class having method name ‘myMethodName’.

Example of Super keyword in Method Overriding:


In this example, we create the Object for Breed class. And ‘super.display()’ calls the ‘display()‘ method from the Dog class that is the parent class. and hence we get ‘I am a Dog‘ result too.

But if we eliminate ‘super.display()‘ then we only get the result as ‘Breed: German Shepard‘.

Method Overloading

Method Overloading is one of the features in a class having more than one method with the same name. It is the same as Constructor Overloading in java.

The methods are differed by its input parameter that is the data-type, number of parameters, and the order of the parameter list.

Note that: Method overloading is not possible by changing the return type of methods.

Now let us see the different ways of overloading methods.

There are two different ways in which the method can be overloaded:

  1. Method overloading, by changing the data type of Arguments.
  2. Method overloading, by changing the number of the argument.

1. Method overloading by changing the data type of Arguments.

In this method, the user needs to change the data-type of passing argument for two different methods of the same name and the parameter list must match with that data-type accordingly.

The following program shows the example of using Method overloading by changing the data type of Arguments in Java:

Output of Method overloading:

2. Method overloading by changing the number of the argument.

Another way is by changing the number of arguments that is the value passed when called. We can also keep the argument empty and it will recognize the method with an empty parameter list.

Following is the example method overloading by changing the number of argument in Java:

Output of Method overloading:


Java Program to check Krishnamurthy Number

In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number …
Read More

Java Program for ISBN Number

In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java …
Read More

C++ Memory Management

Memory management refers to the process of managing the computer memory while assigning the space to the program’s variable to improve the overall performance. Requirement …
Read More

C++ Pointer to Pointer

As we know by now that a pointer stores the address of the pointed variable. But it is not the only use, pointer also stores …
Read More

C++ Passing Pointers to Functions

A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument …
Read More

C++ Null Pointer

We use a null pointer when we do not have the exact address to assign to a pointer. It is considered a good practice and …
Read More