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

  • Sawan ka Mahina : सावन का महीना का महत्व, भगवान शिव की पूजा
    Sawan ka Mahina : सावन का महीना का महत्व, भगवान शिव की पूजा Festival
  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
    Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi Biography
  • Apj Abdul Kalam biography in Hindi, Life, Missile Man of India
    Apj Abdul Kalam biography in Hindi, Life, Missile Man of India Biography
  • Fundamental Duties of Indian Citizens
    Fundamental Duties of Indian Citizens | 11 मौलिक कर्तव्य हिंदी में General Knowledge
  • Fundamental Rights of Indian Citizens
    Fundamental Rights of Indian Citizens | मौलिक अधिकार क्या हैं? General Knowledge
  • Republic day गणतंत्र दिवस | Happy Republic Day
    Republic day गणतंत्र दिवस कब और क्यों मनाया जाता है? Festival
  • P V Sindhu Biography in Hindi
    P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति Biography
  • What is Tense in Hindi (काल क्या है)?
    What is Tense in Hindi (काल क्या है)? Grammar

Method Overloading in Java

Posted on November 5, 2021November 8, 2021 By GeekCer Education 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

Multithreading in java Multithreading in java, Thread, Runnable | Life Cycle of Thread Java
Java Keywords Java Keywords Java
Lambda Expression in Java 8 | Functional Interface | Example Lambda Expression in Java 8 | Functional Interface | Example Java
Variables in Java Variables in Java Java
Top Java Programs for Coder Top Java Programs for Coder Java
Stack Vs Heap in Java Stack Vs Heap in Java | Memory Allocation Section in Java Differences

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
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • 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