Method overloading in Java – Explained

Method overloading is used so that the programmer does not need to define which function is to be called. There are three different ways to overload a method as explained below.

Index

  1. Introduction
  2. History of Java
  3. Method Overloading in Java

 

Introduction 

Java is an object-oriented programming language, It is similar to C and C++, so if you are familiar with these languages learning Java will be easy for you. Java is simple to use; it is platform-independent and multifunctional. In this article, we are going to talk about the method overloading in Java.

 

History of Java

In June 1991, James Gosling, Mike Sheridan, and Patrick Naughton started the Java language program. They wanted it to design for cable TV, but the language was too advanced for cable networks. The language was first named oak; after an oak tree in front of the project office, then it was named green, and later finally, it was named java after java coffee. 

In 1996, Sun Microsystems released the first implementation of Java. It provided the multiplatform feature of Java. After some time, web browsers developed to use Java applets in web pages. In 1997, Sun Microsystems tried to formalize the language, but the initiative was not successful. Now Java is maintained by the Java Community Process. Sun Microsystem generates revenue by selling the license to specialized products. 

Sun Microsystem in 2006 released JVM (Java Virtual Machine) as free, open-source software. Sun Microsystem made it available on open source platforms by 2007, and do not own any copyrights. 

In 2009 – 2010, Oracle acquired Sun microsystem. Java is used everywhere, laptops, computers, game consoles, and many other platforms.

 

Method Overloading in Java

Java is an easy to use language similar to C/C++. The method overloading feature is similar to the function overloading feature of C++. Method overloading allows the function to have the same name as long as their parameters are different. This method is similar to the constructor overloading of Java. Method overloading is an implementation of static overloading. 

In a layman’s language, Polymorphism is the art of using the same name for two different things. Static polymorphism is a compile-time binding example. It happens when the code is being compiled, so if there is an error it notifies during the compilation of the code. 

Method overloading is used so that the programmer does not need to define which function is to be called. The compiler decides on its own which function to call based on the parameters provided. For example, if your program calculates the area of a circle and a rectangle. Both area functions will have different parameters as rectangle needs length and breadth; the circle needs just radius. 

 

There are three different ways to overload a method:

1. You can change the number of parameters –

add(float, float)

add(float, float, float)

EXAMPLE:

class DisplayOverloading

{

    public void display(int c)

    {

         System.out.println(c);

    }

    public void display(int c, int num)  

    {

         System.out.println(c + ” “+num);

    }

}

class Sample

{

   public static void main(String args[])

   {

       DisplayOverloading obj = new DisplayOverloading();

       obj.display(‘2’);

       obj.display(‘2’,10);

   }

}

Output:

2

2 10

 

2. You can change the data type of the parameters –

add(float, float)

add(float, char)

Below is an example code of method overloading using different data types but the same number of parameters. The functions display accepts either float argument or a char argument. 

EXAMPLE: 

class DisplayOverloading2

{

    public void display(float c)

    {

        System.out.println(c);

    }

    public void display(char c)

    {

       System.out.println(c );

    }

}

class Sample2

{

    public static void main(String args[])

    {

        DisplayOverloading2 obj = new DisplayOverloading2();

        obj.display(10.0);

        obj.display(5);

    }

}

Output:

10.0

5

 

3. You can change the sequence of parameters –

add(float, char)

add(char, float)

EXAMPLE: 

class DisplayOverloading3

{

   public void display(char c, int num)

   {

       System.out.println(“Character first”);

   }

   public void display(int num, char c)

   {

       System.out.println(“Number first” );

   }

}

class Sample3

{

   public static void main(String args[])

   {

       DisplayOverloading3 obj = new DisplayOverloading3();

       obj.display(‘x’, 51 );

       obj.display(52, ‘y’);

   }

}

Output:

Character first

Number first

While overloading one should be careful about some rules, or else the overloading will be invalid. In method overloading if the function has the same parameter but different return, it is considered invalid. 

int add(int, int)

Char add(int, int)

This example will throw an error because the compiler will not be able to decide which function is being called based on the parameters. 

Vineet Chaudhary
Vineet Chaudhary
Vineet Chaudhary is a writer with computer applications as his background field. His interests range from writing and photography to going out for trips and rides on weekends.

Leave A Reply

Please enter your comment!
Please enter your name here

Get Tech & Startup Tips
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.