Method Overloading and Overriding in Java With Examples

In Java, method overloading and method overriding are the two ways that are used in order to achieve polymorphism. In other words, overriding and overloading help to perform a single task in several different ways. The ways help a single object to take several different forms. Both methods overriding and method overloading are explained in detail below.

Method Overloading

Method Overloading in Java refers to the case in which a single class contains several different methods with similar names but the parameters are distinct from each other.

The method overloading facilitates different methods to contain similar names but with distinct parameters. Therefore, it is used in Java in order to figure out the program as fast as possible.

The number of the input parameters or the kind of the input parameters can be used to differentiate the signature.

Overloading in Java allows the users to increase the readability of the program as only a single operation that has a similar name as the methods have to be carried out.

Since there is no requirement to construct and remember different names for functions performing the same task in method overloading, it makes the program easily readable. This is the main advantage of using method overloading in Java.

There are two ways in Java that are used in order to carry out method overloading. The first way is to change the number of arguments and the second way is to change the type of data.

However, method overloading cannot be performed in Java by only changing the return type of the method. This is because of ambiguity. Method overloading can also be performed in the main methods in order to get any number of main methods in a class.

A single type can be promoted to another type implicitly by using method overloading in the case in which there is no presence of matching data type.

Example:

Output: 100, 100, 100.0

Method Overriding

Method overriding in Java refers to the situation in which a child class or a subclass has a similar method as that of the superclass or the parent class. So to explain it differently, method overriding is when a subclass contains the same method as that of the superclass.

Overriding is a feature that facilitates the subclass to perform a particular method that the superclass has already performed. The overridden method cannot be viewed in the parent class and is not invoked until the super keyword is used in the child class.

It is the case in which a subclass contains the same name, return type, and signature as a method in its superclass. The main function of method overriding in Java is to allow the user to achieve polymorphism.

Another function of overriding is to specify methods that will be common to all of its subclasses and that has already been given by the superclass. It is also used in Java to call methods of any child class even without the idea of the type of the subclass object.

However, there are a few rules concerned with method overriding in Java. The first one is that the method in a child class should not have a different name as in the parent class.

The next thing to remember is that the method in a child class should have the similar parameter as of the parent class. In order to carry out method overriding in Java, there must be the presence of IS-A relationship otherwise it is not possible.

The last rule to take into consideration while performing method overriding is that the private methods and the final methods can never be overridden. Similarly, method overriding can also not be carried out in a static method. This can be proved through runtime polymorphism.

The reason that method overriding cannot be carried out in a static method is that the method is bound with the class. The static method is contained in the class area not in the object area; therefore, a static method cannot be overridden. Method overriding can also not be performed in the main method of the Java because it is also a static method and belongs to the class area.

Example:

Output:

Hi, I am from the Parent class.
Hi, I am from the Child class.

Method overriding and method overloading

Although both method overriding and method overloading are used in Java in order to achieve polymorphism, both the ways are different from each other in some factors. The major differences between method overriding and method overloading are given in the following table:

Method overriding Method overloading
In method overriding, there is the same method with a similar signature but with different classes. In method overloading, there is the same method with distinct signatures.
Method overriding allows users to achieve runtime polymorphism in Java. Method overloading allows users to achieve compile-time polymorphism.
The main advantage of method overriding is that it specifies methods that will be same to all of its subclasses and that has already been given by the superclass. The main advantage of method overloading is that it increases the readability of the program.
Method overriding is carried out in two different classes that have IS-A relationship between them. Method overloading is carried out within a single class.
In method overriding, the parameter should be similar. In method overloading, the parameter should be different.

Related posts:

Abstract Class Vs Interface in Java With Examples

String to int in Java – Convert int to String

Collections in Java

Garbage Collection in Java

Multithreading in Java

Exception Handling in Java

Leave a Reply

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