Method overloading in java

Home  >  Articles  >  Method overloading in java

Method overloading in java

Shakshi Sehrawat

Updated on 03rd July, 2024 , 3 min read

Method overloading in Java 

The Java programming language paraded onto the scene fully embracing the concept of the OOP-(Object-Oriented Programming).In the Object-Oriented Programming, one of the groovy moves is Polymorphism. It is a dedicated way of saying that objects and methods in your program can hoof in different ways depending on the circumstances. In easier words we say that , a method with the same name can do different actions and can pull out various results, all based on the parameters. 

In Java, the Method Overloading allows the nonidentical methods to have the same name, but dissimilar signatures where the signature can vary by the no. of input arguments, type of input arguments, or a mixture of both. Let's assume that a class has many methods of having the same name but vary in parameters, it is called Method Overloading and if we have to carry on only one operation, then having the same name of the methods enhance the readability and reusability of the program.

Method overloading and method overriding: what's the difference ? 

In Java, besides method overloading, there is another method similar to it which is called method overriding. The latter is also connected to polymorphism, but they serve various purposes and have dissimilar rules, which is also related to another important OOP-(Object-Oriented Programming) concept - inheritance.

So method overriding is a feature that enhances a subclass to provide a specific implementation for a method that is already in its parent class. This is a basic part of the inheritance concept in OOP -(object-oriented programming), allowing the runtime polymorphism.

Here is a small code example. Let's create the class 'Musical Instrument' and two subclasses: 'Piano' and 'Violin'. Both the instruments are meant for playing, but playing a different technique for each. One will override the parent method play() in both subclasses to illustrate the concept.

 What is method overloading in Java?

In Java, Method Overloading allows various methods to have the same name, but vary signatures where these signatures can differ by the no. of input parameters and types of input arguments, and a mixture of both.

In Method overloading in Java is also called Compile-time Polymorphism, static polymorphism, and early binding. In Method overloading compared to the parent argument, the child argument also gets the highest priority.

 

Also see: Collection framework in java

Different ways of overloading in Java?

There are three different ways of overloading in Java:

  • Changing the no of arguments: Method overloading can be achieved by changing the number of arguments while passing to different methods.
  • Changing the data types of the parameters: Methods can be considered Overloaded if they have the exact name but have different parameter types, methods are considered to be overloaded.
  • Changing the method’s of parameters: Method overloading can also be applicable by reordering the parameters of two or more overloaded methods. Let's say, if the parameters of method 1 are (Name, int roll_no) and the other method is (int roll_no, Name) but both have the exact name, then these 2 methods are likely to be overloaded with different sequences of the parameters.

Advantages of Method Overloading 

The following are the advantages of method overloading:

  • Method overloading improves the readability and reusability of the programs.
  • Method overloading minimizes the complexity of the programs.
  • Using method overloading, programmers can perform a task more efficiently and more effectively.
  • Using method overloading, it can be possible to access the methods of performing related functions with slightly different arguments and types.
  • Objects of a class can also be initialized in various ways using the constructors.

Is it possible to overload java main() method?

Yes, by method overloading one can overload the java main()method. One can have whichever number of main methods in a class by the method overloading. But JVM calls the main() method which gets the string array only as arguments.

 

Also see: Abstract keyword in Java

Frequently Asked Questions

what is meant by Method overloading in java ?

The Java programming language paraded onto the scene fully embracing the concept of the OOP-(Object-Oriented Programming).In the Object-Oriented Programming one of the groovy moves is Polymorphism.

What is meant by method overloading in java?

In Java, Method Overloading allows various methods to have the same name, but vary signatures where these signatures can differ by the no. of input parameters and types of input arguments,and a mixture of both.

What are the advantages of method overloading?

Method overloading improvises the readability and reusability of the programs. Method overloading minimizes the complexity of the programs. Using method overloading, programmers can perform a task more efficiently and more effectively.

Is it possible to overload java main() method?

Yes, by method overloading one can overload java main()method. One can have whichever number of main methods in a class by the method overloading.

Check Eligibility   Free 1:1 Counselling