×

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 9 Tutorial

The Java 9 is most popular release of java programming to make it platform modular. It is used to improve JDK and java implementation security. It provides JAVA SE and...

3 minutes read.

Sierpinski Number in Java

The Sierpinski triangle—is it a fractal? The Sierpinski Triangle fractals. A self-similar fractal is the Sierpinski triangle. It is made of an equilateral triangle with its residual area successively reduced by...

3 minutes read.

Java protected vs private

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

Literals in Java

In this article we acknowledge ourselves about the term literals in java, types of literals and an example to each of them. Literal Literal refers to any constant value that can be...

4 minutes read.

How to Round Double Float up to Two Decimal Places in Java

It indicates 15 digits just after the decimal place in Java whenever a double data type is used in front of a variable. For example, when representing rupees and other...

4 minutes read.

Java Class Name

How to write a class name? The following considerations should be made while writing class names. The name of the current class shouldn't be based on a preset or existing class. Java keywords...

3 minutes read.

Java Volatile Keyword

The compiler, runtime, or processors may use any kind of optimization if there aren't any required synchronizations. Although most of the time these improvements are advantageous, they occasionally can result...

6 minutes read.

Java Characters

Normally, when we work with characters, we use primitive data types char. When we have to work with the objects of char, we use Character class. Character class has many important...

2 minutes read.

How to Change the Day in the Date using Java?

To operate with the date and time in Java, we need the Calendar abstract class. It provides a number of helpful interfaces that enable us to convert dates between a...

4 minutes read.

CRC Program in Java

The acronym CRC stands for Cyclic Redundancy Check. It is invented by W. Wesley Peterson in 1961. It is an error detection technique that detects errors in digital networks (also...

5 minutes read.

How to Convert String to Integer in Java

How to convert String to int in Java You need to convert String into int if you want to perform a mathematical operation on string which contains digits. To do so,...

3 minutes read.

Thread Scheduler in java

Scheduling: it is defined as the execution of multiple threads on a single CPU in some order is called scheduling. Preemptive-priority scheduling: This algorithm schedules threads based on their priority relative to other...

11 minutes read.

Anagram Program in Java using String

Anagram Program in Java Anagrams are those words that are generated by rearrangement of the letters of another phrase or word. Usually, while doing the rearrangement, the original letters are used...

8 minutes read.

How to Calculate the Time Difference between Two Dates in Java?

The date is used extensively in Java to calculate date discrepancies. The date of joining an organization, admittance, appointment, etc., can be included when creating the application. The differences between...

4 minutes read.

Design of JDBC

Java applications may interface using database systems from many vendors using the Java Database Connectivity (JDBC) Application Software Interface (API) from Sun Microsystem. To connect spreadsheets, JDBC and database drivers...

3 minutes read.

How to Read XML Files in Java?

Introduction Today, we are going to learn about how to read XML files in java. Before, reading the XML Files let us learn about XML. XML Files The full form of XML is...

8 minutes read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.

Java Variable

The variable is the basic unit of storage in a program. We define a variable using an identifier, a type, and an optional initializer in Java. In Java, variables must be...

4 minutes read.

Static Array in Java

In this tutorial, we will study static arrays in Java. An array is a data structure that is of great importance in any programming language. It is classified into two...

3 minutes read.

Java Tokens

Classes and methods are included in the Java program. The procedures also provide the expressions and statements required to finish a specific operation. Tokens make up the sentences and expressions...

4 minutes read.