The IoT Academy Blog

What is Overloading in Java?

  • Written By  

  • Published on January 23rd, 2023

Table of Contents [show]

 

Introduction

The meaning of overloading is serving one or more functions with a similar name. Java provides the ability to overload methods. Method overloading is a single class that can have multiple methods with a similar name, but they should be distinct in a signature or the number of parameters and return type of the method. This method of overloading functionality advantages codes readability and reusability of the program.

 

Method Overloading lets different methods have a similar name but distinct signatures where the signature can be distinct by the number of input parameters or type of input parameters or a combination of both. Method overloading is also called Compile-time Polymorphism, Static Polymorphism, or Early binding in Java. Method overloading in comparison to the parent argument, the child argument will get the most elevated significance. 

 

Example of Method Overloading 

Here is an example of how method overloading works in Java

 

// Java program to demonstrate working of method

// overloading in Java

 

public class Sum {

 

// Overloaded sum(). This sum takes two int parameters

public int sum(int x, int y) { return (x + y); }

 

// Overloaded sum(). This sum takes three int parameters

public int sum(int x, int y, int z)

{

return (x + y + z);

}

 

// Overloaded sum(). This sum takes two double

// parameters

public double sum(double x, double y)

{

return (x + y);

}

 

// Driver code

public static void main(String args[])

{

Sum s = new Sum();

System.out.println(s.sum(10, 20));

System.out.println(s.sum(10, 20, 30));

System.out.println(s.sum(10.5, 20.5));

}

}

 

OUTPUT:

30

60

31.0

 

Our Learners Also Read: What is an Array in Java?

 

 

Constructor Overloading in Java

 

In Java, we can overload constructors like methods. Constructor overloading can be described as the concept of holding more than one constructor with distinct parameters so that each constructor can complete a different assignment. Here, we require to comprehend the objective of constructor overloading. Sometimes, we require to use multiple constructors to initialize the other values of the class.

We must also see that the java compiler summons a default constructor when we do not operate any constructor in the class. In this case, the java compiler throws an exception saying the constructor is unclear. 

 

Features of Method Overloading

 

Some of the Features of Method Overloading are mentioned below:

 

  • Since the method overloading is static Polymorphism, i.e. methods are connected during the compilation procedure.
  • Overloading influences them at runtime, and binding of methods is done at compile-time, so many procedures are not needed during run time, i.e. binding or checking.
  • Only modifying the return of the method does not assume method overloading and thus results in an ambiguity error.
  • The number of arguments, order of arguments, and type of arguments are part of the fundamental method of overloading.

 

Advantages of Method Overloading

Some of the advantages of method overloading are mentioned below:

 

  • Method overloading enhances the readability and reusability of the program.
  • Method overloading decreases the complexity of the program. Hence provides consistency of code.
  • Using method overloading, programmers can achieve a task efficiently and effectively.
  • Using method overloading, it is likely to access methods executing related functions with little different arguments and types.
  • Method overloading decreases execution time because the binding is done during compilation time.
  • An overloading mechanism acquires flexibility.

 

Conclusion

 

By using the method overloading mechanism, static polymorphism is obtained. Method overloading decreases code complexity and controls writing different methods for the same functionality with a distinct signature. The reusability of code is acquired with overloading since a similar method is used for distinct functions.

 

full stack java developer course

 

 

About The Author:

logo

Digital Marketing Course

₹ 9,999/-Included 18% GST

Buy Course
  • Overview of Digital Marketing
  • SEO Basic Concepts
  • SMM and PPC Basics
  • Content and Email Marketing
  • Website Design
  • Free Certification

₹ 29,999/-Included 18% GST

Buy Course
  • Fundamentals of Digital Marketing
  • Core SEO, SMM, and SMO
  • Google Ads and Meta Ads
  • ORM & Content Marketing
  • 3 Month Internship
  • Free Certification
Trusted By
client icon trust pilot
1whatsapp