×

Method Overloading In Java

If the same class consists of different methods with the same name and the methods can vary by different number of parameters then it is known as Method Overloading. Method overloading increases the readability of the code or program.

There are two ways to obtain the Method Overloading:

  •  Changing the number of parameters
  • Changing the datatype of parameters

Changing the number of parameters

In this concept, we have same method names with different number of parameters passing through the methods.

 Overloading.java

class Overloading1
{
  int num1;
  int num2;
  int num3;
  int num4;
  int add (int a, int b)
  {
    num1 = a;
    num2 = b;
    return (num1 + num2);
  }
  int add (int a, int b, int c)
  {
    num1 = a;
    num2 = b;
    num3 = c;
    return (num1 + num2 + num3);
  }
  int add (int a, int b, int c, int d)
  {
    num1 = a;
    num2 = b;
    num3 = c;
    num4 = d;
    return (num1 + num2 + num3 + num4);
  }
}
class Overloading
{
 public static void main(String args[])
 {
  Overloading1 obj1 = new Overloading1 ();
  System.out.println (obj1. add (22, 22));
  System.out.println (obj1. add (5, 6, 7));
  System.out.println (obj1. add (5, 6, 7, 8));
}
}

Output:

Method Overloading In Java

 In the above code, add () is the method having different number of parameters once, it takes as two parameters i.e , int a and int b . It also takes three parameters i.e, int a, int b and int c. Here, num1, num2, num3 and num4 are instance variable the local variables a, b, c, d values are stored into instance variables after entering into methods. Here methods are non – static hence in program objects are created i.e, obj1. The methods are called by objects, hence they perform operations in methods and returns output to main method.

Changing the datatype of parameters

In this concept, the method names are same and they have same number of parameters passing through it. But the parameters are having different datatypes.

Parameter.java

class Overloading2
{
  int add (int a, int b)
  {
    int num1 = a;
    int num2 = b;
    return (num1 + num2);
  }
  double add (double a, double b)
  {
    double num1 = a;
    double num2 = b;
    return (num1 + num2);
  }
  float add (float a, float b)
  {
    float num1 = a;
    float num2 = b;
    return (num1 + num2);
   }
}
class Parameter
{
 public static void main (String args[])
{
 Overloading2 obj2 = new Overloading2 ();
 System.out.println ("Datatype int sum :" + obj2. add (5, 5));
 System.out.println ("Datatype Double sum :" + obj2. add (5.3, 5.6));
 System.out.println ("Datatype float sum :" + obj2. add (5.4f, 5.2f));
}
}

Output:

Method Overloading In Java

In the above code, add is the method having same number of parameters, having different datatypes of parameters passing through the method. The various datatypes passing through the methods are int, float, double. Here the methods are instance methods, Hence the objects are created. These methods perform operations and returns output to the main method.


Related Topics

Java Array Generic

Creating Generic Array in Java A collection of comparable sorts of data is kept in an array. In Java, making a generic array is challenging. The type information of an array's...

4 minutes read.

Transient variable in Java

In this article, you will be acknowledged about transient variable along with its functions. We would conclude by understanding an example program about it. Transient variable By introducing the transitory keyword, we...

3 minutes read.

Binary Search Java

Binary search is a search mechanism for key elements from the given List/Array. In Binary search, the search mechanism is followed by dividing the array into parts; hence the search...

3 minutes read.

Trim Method in String Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

3 minutes read.

Java List Interface

List interface is used when we have to order a collection which contains duplicate entries. Like an array, the elements of its implementation classes are retrieved and inserted at a...

2 minutes read.

Web Crawler in Java

In this article, you will be acknowledged with what a web crawler in java is and what are its functions. You will also be able to understand where to implement...

4 minutes read.

Multithreading Program in Java

Multithreading Program in Java: Before discussing multithreading, it is important to discuss threads. Threads are the most fundamental part of a process. A process can have one or more threads....

4 minutes read.

How to Convert long to int in Java

How to Convert long to int in Java When we assign a larger type value to a variable of smaller type, then we need to perform explicit casting for the conversion....

2 minutes read.

Zygodromes in Java

Zygodrome is a positive number created by the same digits running non-trivially. A number is called a zygodrome if identical digits constantly occur together (in pairs). The Greek word "zyg"...

4 minutes read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

Sorting a String in Java

Sorting a String in Java Sorting is a process of arranging available data objects in a meaningful format that is ascending or descending order. Sorting a string or array of String...

4 minutes read.

List all files in a Directory in Java

The list of all files in the directory can be done using Java. You should be aware that a directory may include a subfolder and that subdirectory may also contain some...

3 minutes read.

Composition in Java

Composition Java uses the composition method to implement a has-a connection. Composition allows us to reuse code in the same way that Java inheritance does. The "is-a" relationship is implemented using the...

3 minutes read.

Java LinkedHashSet

LinkedHashSet in Java with Example Java LinkedHashSet extends HashSet and Implements the Set interface. It doesn’t contain only duplicate values like HashSet. It also permits the null elements. It maintains the order...

5 minutes read.

Java Flags Enum

In a programming language, enumerations represent a group of named constants.For instance; the four suits in a deck of playing cards could be the enumerators Club, Diamond, Heart, and Spade,...

3 minutes read.

Java Integer compare() method

The compare() method of Integer class compares the two specified int values. Syntax public static int compare(int x, int y) Parameters The parameters ‘x’ and ‘y’ represent the first and second int values to...

2 minutes read.

Java Transient

Java Transient In Java, Serialization is used to convert an object into a stream of the byte. The byte stream consists of the data of the instance as well as the...

3 minutes read.

Pangram Program in Java

If a string comprises all alphabet letters from A to Z or from a to z without regard to case, it is referred to as a pangram. Some examples of pangram...

3 minutes read.

Java Math sin() Method

The sin() method of Java Math class returns the trigonometric sine of the specified angle. Syntax: public static double sin(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The sin() method...

1 minute read.

Three Partition Problems in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

8 minutes read.