07
DecMethod Overloading And Overriding In Java (With example)
Method Overloading Vs. Overriding In Java
Overloading and Overriding are two basic ideas that are different in several various ways in Java. Compile-time polymorphism is achieved by overloading, which allows multiple methods inside the same class to have the same name but different parameters (type, number, or both). On the other hand, overriding offers runtime polymorphism when a subclass offers a particular implementation of a method that is already specified in its superclass.
In this Java tutorial, we will explore more about Overloading and Overriding in Java and we are going to more focus on the difference between method overloading and overriding In Java. So let's understand first what is method overriding and what is method overloading.
Take the leap into full stack development—join our Java Full Stack Developer Training for expert guidance.
What is Method Overriding in Java?
- In method overriding a subclass can override a method that is specified in its superclass by providing a customized implementation.
- It makes polymorphism in Java possible by allowing a subclass to specify its own implementation of a function.
- One kind of runtime polymorphism is method overriding.
- Method overriding is the process of giving a method in a derived class the same name, parameters, and return type as a method in the parent class.
- For a method that is already defined in the parent class, the derived class offers a particular implementation.
Key Characteristics
- Inheritance Requirement: A superclass-subclass relationship is necessary for overriding.
- Same Method Signature: The name, parameters, and return type of overriding methods are all the same.
- Runtime Polymorphism: The object type determines at runtime which method should be used.
Examples of Method Overriding
// Superclass
class Company {
void NoofEmployees() {
System.out.println("Number of Employees");
}
}
// Subclass overriding method
class Employee extends Company {
@Override
void NoofEmployees() {
System.out.println("There are 50 employees");
}
}
// Main class to demonstrate method overriding
public class Main {
public static void main(String[] args) {
Company comp = new Company(); // Create Company object
comp.NoofEmployees(); // Output: NumberofEmployees
Company emp = new Employee(); // Company reference but Dog object
emp.NoofEmployees(); // Output: There are 50 employees
}
}
Output
NumberofEmployees
There are 50 employees
Explanation
- Company Class: This is the superclass with a method NoofEmployees() that prints "Number of Employees".
- Employee Class: Extends Company and overrides the NoofEmployees() method to print " There are 50 employees" instead.
- Main Class: In the main method: A Company object ( comp) calls NoofEmployees(), which prints "Number of Employees".
- An Employee object is assigned a reference of type Company ( employee). When employee. NoofEmployees() is called, it dynamically binds to the NoofEmployees() method in the Employee class due to method overriding, printing " There are 50 employees."
Usage
- Used in inheritance to implement certain behaviors in subclasses.
- It offers specific implementations of the methods used by abstract classes or interfaces.
What is Method Overloading in Java?
- The ability to define multiple methods with the same name but different parameters in a class is known as method overloading in Java.
- These techniques improve the flexibility and clarity of the code by accomplishing comparable tasks with different inputs.
- Compile time polymorphisms include method overloading.
- Although the return type of the method may or may not remain the same, in Java, you can not achieve method overloading by changing only the return type of the method.
Key Characteristics
- Same Method Name: The names of the overloading methods are the same.
- Different Parameters: Parameters might be different in terms of their type, number, or both.
- Compile Time Resolution: The method signature is used to determine how overloading is resolved at build time.
Examples of Method Overloading
public class Calculator {
// Method to add two integers
public int add(int a, int b) {
return a + b;
}
// Method to add three integers
public int add(int a, int b, int c) {
return a + b + c;
}
// Method to add two doubles
public double add(double a, double b) {
return a + b;
}
// Method to concatenate two strings
public String add(String a, String b) {
return a + b;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
// Calling different overloaded methods
int sum1 = calc.add(5, 10);
int sum2 = calc.add(5, 10, 15);
double sum3 = calc.add(2.5, 3.5);
String concatenatedString = calc.add("Hello, ", "World!");
// Displaying results
System.out.println("Sum of integers (5 + 10): " + sum1);
System.out.println("Sum of integers (5 + 10 + 15): " + sum2);
System.out.println("Sum of doubles (2.5 + 3.5): " + sum3);
System.out.println("Concatenated string: " + concatenatedString);
}
}
Output
Sum of integers (5 + 10): 15
Sum of integers (5 + 10 + 15): 30
Sum of doubles (2.5 + 3.5): 6.0
Concatenated string: Hello, World!
Explanation
In this instance:
- Two integers are added using add(int a, int b).
- Three integers are added with add(int a, int b, int c).
- Two doubles are added using add(double a, double b).
- Two strings are concatenated with add(String a, String b).
Usage
- It provides several methods (such as add(int, int) and add(double, double)) for calling a method with various parameter types.
- It is used to define constructors for object initialization that take distinct argument lists.
Differences Between Overloading and Overriding in Java
Method Overloading in Java | Method Overriding in Java |
It is a compile-time polymorphism. | It is a run-time polymorphism. |
It helps to increase the readability of the program. | It is used to give access to the specific implementation of the method which is already provided by its parent class or superclass. |
Method overloading occurs within the class only. | Method overriding is performed in two classes with inheritance relationships. |
It may or may not require inheritance. | It always needs inheritance. |
In this, methods must have the same name and different characteristics | In this, methods must have the same name and same characteristics. |
In this, the return type can or can not be the same, but you have to change the parameter. | In this, the return type must be the same or co-variant. |
Static binding is used in method overloading. | In method overriding dynamic binding is used. |
In this, Private and final methods can be overloaded. | In this, private and final methods can’t be overridden. |
The parameters should be different while doing method overloading. | The parameters should be the same in method overriding. |