Java-Overloading
Overloading is a rule where multiple methods and constructors with the same name are defined together within the class. These methods differ in the number or type of parameters, making code more readable and efficient.
Types of Overloading in Java:- Constructor Overloading
- Method Overloading
Constructor Overloading:Constructor overloading is the concept in which a class has multiple constructor with the same name but are different in terms of parameters and data type of parameters. It enables you to create objects with different initializations based on the given parameters.
Why Use Constructor Overloading ?
- Allows flexibility while creating objects.
- Enables different ways to initialize an object.
- Improves code readability by avoiding multiple method names for initialization.
Why Use Constructor Overloading ?
- Allows flexibility while creating objects.
- Enables different ways to initialize an object.
- Improves code readability by avoiding multiple method names for initialization.
Example:
package quipoHouse;
class Person {
private String name;
private int age;
public Person(String name) {
this.name = name;
this.age = 0;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Person person1 = new Person("Alice");
Person person2 = new Person("Bob", 30);
person1.displayInfo();
person2.displayInfo();
}
}
Output:
Name: Alice
Age: 0
Name: Bob
Age: 30
Method Overloading:Method overloading is the concept in which a class has multiple methods with the same name but are different in terms of parameter and data type of parameters. It optimizes the readability of the program. Method overloading is similar to constructor overloading, but it applies to methods.
Why Use Method Overloading:
- Increases readability – No need to create separate method names for similar operations.
- Improves code organization – The logic remains grouped under a single method name.
- Enhances maintainability – Changes can be made easily without affecting other parts of the code.
Why Use Method Overloading:
- Increases readability – No need to create separate method names for similar operations.
- Improves code organization – The logic remains grouped under a single method name.
- Enhances maintainability – Changes can be made easily without affecting other parts of the code.
Different Ways to Achieve Method Overloading in Java:
1. Method Overloading by changing the Number of Parameters:
Example: Here, methods have the same name but different numbers of parameters.
package quipoHouse;
public class Overloading {
static int add(int a, int b) {
return a + b;
}
static int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
System.out.println("Method Overloading With different no of parameters :" + Overloading.add(12, 13));
System.out.println("Method Overloading With different no of parameters :" + Overloading.add(12, 13, 14));
}
}
Output:
Method Overloading With different no of parameters : 25
Method Overloading With different no of parameters : 39
2. Method Overloading by Changing the Data Type of Parameters:
Example: Here method have the same name but different data types for parameters.
package quipoHouse;
public class Overloading {
static int add(int a, int b) {
return a + b;
}
static int add(double a, double b) {
return (int) (a + b);
}
public static void main(String[] args) {
System.out.println("Method Overloading With integer data type:" + Overloading.add(12, 13));
System.out.println("Method Overloading With double data type:" + Overloading.add(12.6, 13.5));
}
}
Output:
Method Overloading With integer data type: 25
Method Overloading With double data type:25
Constructor Overloading Vs. Method Overloading
Feature | Constructor Overloading | Method Overloading |
---|---|---|
Purpose | Initializes objects in different ways | Allows multiple methods with the same name |
Return Type | No return type (constructors don't return values) | Can have any return type |
Execution | Called when an object is created | Called when a method is invoked |
Example | Person (String name) & Person (String name, int age) | add (int a, int b) & add(double a , double b) |