Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Java » Method Overloading in Java

  • How to become a Chief Financial Officer (CFO)
    How to become a Chief Financial Officer (CFO)? Important
  • Kumkum Bhagya Hindi TV Serial, Cast Name, Actress Name
    Kumkum Bhagya Hindi TV Serial, Cast Name, Actress Name News
  • What is Tense in Hindi (काल क्या है)?
    What is Tense in Hindi (काल क्या है)? Grammar
  • List Of National Animals Of Countries
    List Of National Animals Of Countries General Knowledge
  • What is WAR file in Java
    What is WAR file in Java? Servlet
  • Structure in C program with example | Nested structure in C
    Structure in C program with example | Nested structure in C C Language
  • Linked List in Data Structure and Algorithm, Types, Operations
    Linked List in Data Structure and Algorithm, Types, Operations C Language
  • Linux Commands With Example | Linux commands cheat sheet
    Linux Commands With Example | Linux commands cheat sheet Important

Method Overloading in Java

Posted on November 5, 2021November 8, 2021 By admin No Comments on Method Overloading in Java
Method Overloading in Java

In Java, method overloading happens when the names of two methods are the same but the arguments are different. Two methods with the same name but different parameters are permitted in Java, and these methods are referred to as overloaded methods.

We must verify method names, which must be the same, and parameters, which must be different, when overloading methods. All other factors, such as return type, throws clause, access modifier, and so on, are not required to be checked.

Table of Contents

  • Advantages of Method Overloading in Java
  • How do you overload a method in Java?
    • By changing the number of method arguments
    • By changing the data types of method arguments
  • Overloading main method in Java
  • Method Overloading in Java real time example
  • Frequently Asked Questions – Method Overloading in Java

Advantages of Method Overloading in Java

  • The readability of the program is improved by method overloading.
  • Because the binding is done during the compilation process, the execution time is reduced.
  • This gives programmers more freedom since they may use the same approach for different kinds of data.
  • Overloading methods reduces the code’s complexity and allow us to reuse the code, which saves memory.

How do you overload a method in Java?

In Java, there are two ways to overload a method.

  1. Number of Arguments: By changing the number of method arguments
  2. Argument Data Types: By changing the data types of method arguments

By changing the number of method arguments

Here’s an example in which we’ve constructed two methods that will perform number addition. The first method will perform a two-number addition, whereas the second method will perform a three-number addition.

public class MethodOverloadingDemo {
	public void addition(int num1, int num2) {
		System.out.println("Addition of two numbers: " + (num1 + num2));
	}

	public void addition(int num1, int num2, int num3) {
		System.out.println("Addition of three numbers: " + (num1 + num2 + num3));
	}

	public static void main(String[] args) {
		MethodOverloadingDemo obj = new MethodOverloadingDemo();
		obj.addition(10, 20);
		obj.addition(10, 20, 30);
	}
}
Output:
Addition of two numbers: 30
Addition of three numbers: 60

By changing the data types of method arguments

Here’s another example, in which we’ve constructed two techniques for performing number addition. The addition of two integer type data will be performed by the first method, while the addition of two double type data will be performed by the second method.

public class MethodOverloadingDemo {
	public void addition(int num1, int num2) {
		System.out.println("Addition of int numbers: " + (num1 + num2));
	}

	public void addition(double num1, double num2) {
		System.out.println("Addition of double types: " + (num1 + num2));
	}

	public static void main(String[] args) {
		MethodOverloadingDemo obj = new MethodOverloadingDemo();
		obj.addition(10, 20);
		obj.addition(10.20, 20.30);
	}
}
Output:
Addition of int numbers: 30
Addition of double types: 30.5

Overloading main method in Java

Can we overload main method in Java? Yes, we can overload main method in Java. This is the most important topic of Java. It’s also a popular question in the interview room.

In Java, we can overload the main() method with different numbers and types of parameters, but the JVM only understands the main() method, which has just one “String[]” type argument.

public class MainMethodOverloading {
	public static void main(int args) {
		System.out.println("Integer type argument");
		System.out.println(args);
	}

	public static void main(char args) {
		System.out.println("Character type argument");
		System.out.println(args);
	}

	public static void main(Double[] args) {
		System.out.println("Double Array Type");
		System.out.println(args);
	}

	public static void main(String[] args) {
		System.out.println("Original main method");
		MainMethodOverloading.main(100);
		MainMethodOverloading.main('A');
		MainMethodOverloading.main(1000);
		MainMethodOverloading.main(new Double[]{10.25});
	}

}
Output:
Original main method
Integer type argument
100
Character type argument
A
Integer type argument
1000
Double Array Type
[Ljava.lang.Double;@2a139a55

Method Overloading in Java real time example

Any ecommerce website’s payment choices include netbanking, COD, credit card, and so on. This means that a payment mechanism is overloaded several times in order to accomplish a single payment function in multiple ways.

public class Payment {

	public void getPayment() {
		System.out.println("Cash on Delivery");
	}

	public void getPayment(String netBankingId, String Password) {
		System.out.println("Payment by using netbanking");
	}

	public void getPayment(String name, String creditCarNo, String expiryDate, int cvvNo) {
		System.out.println("Payment by using Credit card");
	}

	public static void main(String[] args) {
		Payment payment = new Payment();
		payment.getPayment();
		payment.getPayment("2302323", "Geek202*");
		payment.getPayment("Geek Kumar", "3288-2113-2003-0000", "10/10/2022", 000);
	}
}
Output:
Cash on Delivery
Payment by using netbanking
Payment by using Credit card

Frequently Asked Questions – Method Overloading in Java

  • What is method overloading in Java?
  • What is method overloading and overriding in Java?
  • When two methods have the same name but different parameters, what is it called?
  • What are override methods?
  • What happens if two methods have the same name, the same parameters, but return types that aren’t the same?
  • Is it possible to have two or more methods with the same name but different parameter lists?

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Java

Post navigation

Previous Post: What is Array in Java? Types of array in Java
Next Post: Object Oriented Programming

More Related Articles

JDBC Driver in Java JDBC Driver in Java Java
Java 11 new Features (With example Programs) Java 11 new Features (With example Programs) Important
Java 8 interview questions and answers, Java Stream, Optional Java 8 interview questions and answers, Java Stream, Optional Important
Java Vector Java Vector Java
Difference between StringBuffer and StringBuilder Difference between StringBuffer and StringBuilder Differences
Multithreading in java Multithreading in java Java

Related Posts

  • JDBC Driver in Java
    JDBC Driver in Java Java
  • Java 11 new Features (With example Programs)
    Java 11 new Features (With example Programs) Important
  • Java 8 interview questions and answers, Java Stream, Optional
    Java 8 interview questions and answers, Java Stream, Optional Important
  • Java Vector
    Java Vector Java
  • Difference between StringBuffer and StringBuilder
    Difference between StringBuffer and StringBuilder Differences
  • Multithreading in java
    Multithreading in java Java

Leave a Reply Cancel reply

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

  • Java Home
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Keywords
  • Java Operators
  • Java If-else Statement
  • Java Switch
  • Java Loop
  • Java Arrays
  • Method Overloading in Java
  • Java OOP
  • Java Collections
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Multithreading in java
  • Thread Synchronization
  • Exception Handling
  • Java JDBC Driver
  • Java Database Connectivity steps
  • Lambda Expressions
  • Concurrent Collections

Recent Posts

  • Structured Vs Unstructured Data in Hindi | Key Difference
  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
  • Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth
  • World Environment Day in Hindi : Objective, Importance, Theme
  • Thomas Edison Biography in Hindi – थॉमस एडिसन जीवनी
  • International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं?
  • Fork/Join Framework in Java | RecursiveTask, RecursiveAction
  • DBMS in Hindi | DBMS क्या है? | DBMS की विशेषताएं और प्रकार
  • How to become a Chief Financial Officer (CFO)
    How to become a Chief Financial Officer (CFO)? Important
  • Kumkum Bhagya Hindi TV Serial, Cast Name, Actress Name
    Kumkum Bhagya Hindi TV Serial, Cast Name, Actress Name News
  • What is Tense in Hindi (काल क्या है)?
    What is Tense in Hindi (काल क्या है)? Grammar
  • List Of National Animals Of Countries
    List Of National Animals Of Countries General Knowledge
  • What is WAR file in Java
    What is WAR file in Java? Servlet
  • Structure in C program with example | Nested structure in C
    Structure in C program with example | Nested structure in C C Language
  • Linked List in Data Structure and Algorithm, Types, Operations
    Linked List in Data Structure and Algorithm, Types, Operations C Language
  • Linux Commands With Example | Linux commands cheat sheet
    Linux Commands With Example | Linux commands cheat sheet Important
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved