• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Java Program
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » Method Overloading & Different Ways to Overloading

Method Overloading & Different Ways to Overloading

Last Updated on: August 15, 2020 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Method Overloading in Java
  • Example Of Method Overloading
  • Why Method Overloading?
  • Different Ways to overloaded a Method
  • Important Points
  • Write a Program to Find out Main Method Overloading Java Example Program?

Method Overloading In Java: I Hope this core Java tutorials series helps you to understand the Java concepts easily, if still, you have not checked our Java tutorials then you can use this link to check all the posts regarding core Java topics and also you can learn how to implement because we simply explain this, in this post we are going to learn about method overloading concept in Java programming language.

Method Overloading in Java

In Java, if two or more methods have the same name but they have different parameters or arguments, then such types of methods are called overloaded methods, and that feature is called Method Overloading. It is much similar to the constructor overloading because a class has more than one constructor, but all constructor parameters are different.

Example Of Method Overloading

void test() {..} 
int test(int a) {..} 
float test(float b) {..} 
void test(int a, float b) {..}

In the above code, if you see all the method names is the same, but the parameters are different. So we can say that the test() method is overloaded.

Note: If you have noticed the above methods, then you can find that all the methods return type is not the same, that means in overloaded methods, the method name is the same, but they may be different in parameter and return type.

Method Overloading In Java Explanation
Method Overloading In Java Explanation

Why Method Overloading?

Let’s take a simple example to understand the requirement of method overloading. As you know to operate a TV, we require a Remote but for the different company TV’s there is a chance of the Remote may be looking different but they all are used for the same purpose that is to operate the TV.

If you think about the same thing in a programming language, then the operation performed by a Remote is the same, but the Remote is looking different. If for each company remote, if you are going to create a separate class and methods, then there may duplicate methods are created for the same operation.

In the above case, we can accomplish the same thing in a better way by overloading methods, and depending on the argument, the specific overloaded methods are called. This helps in increasing the readability of the program and re-usability of code also.

Different Ways to overloaded a Method

We Can Overload the methods in the following ways:

  • The number Of Parameter Different: If two method has the same name but different parameter, then that’s a valid case of method overloading.
void test(int);
void test(int, int);
  • Parameter Data Type Should be Different: If the number of parameters is the same, but the data type is the same, then its also a valid example of Method overloading.
void test(int,int);
void test(float, int);
  • Sequent of Data type is different: If Two methods have the same name and the same number of the argument but the arguments data type order is different, that’s also a valid case of method overloading.
void test(int,float);
void test(float, int);
  • InValid Case of Method Overloading: When Two or More Methods have the same name and the same number of the argument but different method return type, then that’s not a valid example of method overloading, in this case, you will get a compile-time error.
int test(int, int);
float test(int, int);

Note: Method overloading is an example of Static polymorphism. More details of polymorphism we are discussing here.

package com.SoftwareTestingo.JavaBasics;
class ParentClassEx
{
   public void displayString(String str)
   {
      System.out.println("Single Parameterized Display Method Executed");
   }
   public void displayString(String str, String str1) {
      System.out.println("Double Parameterized Display Method Got Executed");
      
   }
}
public class MethodOverloadingEx1 
{
   public static void main(String[] args) 
   {
      PolymorphismEx obj=new PolymorphismEx();
      obj.displayString("Welcome"); // Will Call Single Parameterized Method
      obj.displayString("WELCOME", "USER");// Will Call Double Parameterized Method
   }
}

Output:

Single Parameterized Display Method Executed
Double Parameterized Display Method Got Executed

Important Points

Static Polymorphism or Method Overloading also called compile-time binding or early binding because binding of methods happens at compile time.

I hope after going through this article, you can get a clear idea about the method overloading and how we can use this in our script. If still, you have any doubts then you can ask us by dropping a comment in the comment section

Write a Program to Find out Main Method Overloading Java Example Program?

package com.java.Softwaretestingblog;
public class MainMethodOverloading {

   public static void main(int a) 
   {
      System.out.println("Main Method Overloading Done");
   }
   @SuppressWarnings("static-access")
   public static void main(String[] args) 
   {
      System.out.println("Main method ");
      MainMethodOverloading obj=new MainMethodOverloading();
      obj.main(20);
   }

}

Output:

Enter A Year
2000
it is leap year

Ref: article

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers