Overloading & Overriding in Java

Method Overloading in Java

It is referred to as method overloading when a class contains several methods with the same name but different parameters.

The program is easier to read if all of the methods have the same name when there is only one operation to be done.

It could be challenging for you and other programmers to understand the behavior of the method if you write it as a(int,int) for two parameters and b(int,int,int) for three. This is because the names of the methods differ. Let’s say you need to perform addition of the given numbers with any number of arguments.

Advantage of method overloading

Method overloading increases the readability of the program.

Different ways to overload the method

There are two ways to overload the method in java

  1. By changing number of arguments
  2. By changing the data type

1) Method Overloading: changing no. of arguments

Two methods are developed in this example: the first add() method adds two integers, while the second add method adds three numbers. 

  • class first
  • {  
  • int add(int a,int b)
  • {

Int c;

c=a+b;

  • return c;
  • }  
  • add(int a,int b,int c)
  • {
  • d=a+b+c;
  • System.out.println(“The Sum of Three Nos is”+d);
  • }  
  • public static void main(String[] args)
  • {  
  • System.out.println(first.add(5,3));  
  • first.add(10,20,30);  
  • }
  • }  

2) Method Overloading: changing data type of arguments

Two distinct methods with different data types have been built in this example. Two integer parameters are given to the first add method, and two double arguments are given to the second add function.

  • class first
  • {  
  • int add(int a,int b)
  • {

Int c;

c=a+b;

  • return c;
  • }  
  • add(double a,double b,double c)
  • {
  • d=a+b+c;
  • System.out.println(“The Sum of Three Nos is”+d);
  • }  
  • public static void main(String[] args)
  • {  
  • System.out.println(first.add(5,3));  
  • first.add(10.5,11.2,5.3);  
  • }
  • }  

Method Overriding in Java

  1. Understanding the problem without method overriding
  2. Can we override the static method
  3. Method overloading vs. method overriding

In Java, it’s referred to as method overriding when a subclass (child class) declares the same method as the parent class.

Stated differently, method overriding occurs when a subclass offers the precise implementation of a method that one of its parent classes has stated.

Rules for Java Method Overriding

  1. The method must have the same name as in the parent class
  2. The method must have the same parameter as in the parent class.
  3. There must be an IS-A relationship (inheritance).

Difference between method overloading and method overriding in java

In Java, there are numerous distinctions between method overloading and method overriding. The following is a list of distinctions between method overloading and method overriding:

No.Method OverloadingMethod Overriding
1)To make the program easier to read, method overloading is employed.The specialized implementation of a method that is already supplied by its super class is offered through method overriding.
2)Overloading methods is done in class.In two classes with an IS-A (inheritance) connection, method overriding takes place.
3)When a method overloads, an alternative parameter is required.When overriding a method, the parameter needs to be the same.
4)One instance of compile time polymorphism is method overloading.One example of run-time polymorphism is method overriding.
5)Java does not support method overloading with a simple change to the method’s return type. When a method overloads, the return type may be the same or differing. However, you’ll need to adjust the setting.Method overriding requires the return type to be the same or covariant.

Leave a Comment

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

Scroll to Top