18
JulMethod Overloading And Overriding In Java (With example)
In this Java tutorial, we will explore more about Overloading and Overriding in Java, including What is Method Overriding in Java?, What is Method Overloading in Java?, Differences Between Overloading and Overriding in Java, and a lot more.
What is Method Overriding in Java?
Method Overriding occurs when a subclass provides its own implementation of a method that is already defined in its superclass, using the same method name, return type, and parameters. It allows a subclass to modify or extend the behavior of inherited methods.
This is an example of runtime (dynamic) polymorphism, where the method call is resolved during program execution based on the object's runtime type.
Key Characteristics of Method Overriding in Java
- Occurs between superclass and subclass (inheritance required)
- Method must have the same name, return type, and parameters
- Represents runtime polymorphism
- Only instance methods can be overridden (not static, private, or constructors)
- Overriding method cannot have a more restrictive access modifier
- Use of @Override annotation is recommended
- Allows custom implementation in the subclass
- Supports covariant return types
Advantages of Method Overriding
- Supports Runtime Polymorphism: Enables dynamic method dispatch based on the object’s runtime type.
- Improves Code Reusability: Reuses parent class logic while allowing flexibility in child classes.
- Enhances Maintainability: Changes to subclass behavior can be made without affecting the superclass.
- Promotes Consistency: Ensures consistent method names across class hierarchies with different implementations.
Provides Custom Behavior: Allows subclasses to modify or extend the functionality of inherited methods.
Disadvantages of Method Overriding
- Can Cause Confusion: Multiple methods with the same name may be confusing to new developers.
- Hard to Debug: Errors in choosing the correct overloaded method can be tricky to identify and fix.
- Not Always Necessary: Overuse of overloading can make the code unnecessarily complex.
- Return Type Alone Isn't Enough: You can’t overload a method just by changing the return type, which can limit flexibility.
- May Increase Maintenance Effort: If too many overloaded versions exist, maintaining them can become difficult over time.
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.
Advantages of Method Overriding
- Provides Custom Behavior: Allows subclasses to modify or extend the functionality of inherited methods.
- Supports Runtime Polymorphism: Enables dynamic method dispatch based on the object’s runtime type.
- Improves Code Reusability: Reuses parent class logic while allowing flexibility in child classes.
- Enhances Maintainability: Changes to subclass behavior can be made without affecting the superclass.
- Promotes Consistency: Ensures consistent method names across class hierarchies with different implementations.
Disadvantages of Method Overriding
- Can Lead to Confusion: If not used carefully, it may be unclear which method is being executed at runtime.
- Tight Coupling: Creates dependency between subclass and superclass, which can reduce flexibility.
- Increased Complexity: Understanding program flow becomes harder, especially in large inheritance hierarchies.
- Risk of Incorrect Behavior: If the overridden method doesn't align with the intended behavior, it may introduce bugs.
- Performance Overhead: Runtime polymorphism may slightly impact performance compared to compile-time binding.
What is Method Overloading in Java?
Method Overloading allows you to define multiple methods in the same class with the same name but different parameter lists (varying in number, type, or order of parameters). It helps in designing methods that handle different types of data inputs while maintaining a consistent method name.
This is an example of compile-time (static) polymorphism, where the method call is resolved at the time of compilation.
Key Characteristics of Method Overloading in Java
- Occurs within the same class
- Represents compile-time polymorphism
- Return type can vary, but cannot overload by return type alone
- Helps in writing cleaner and more readable code
- Can also apply to constructors (Constructor Overloading)
- Improves code reusability by grouping related logic under one method name
- Same method name, but different parameter lists (type, number, or order)
Advantages of Method Overloading
- Improves Code Readability: Same method name used for related operations makes the code easier to understand.
- Increases Flexibility: Allows the method to handle different types or numbers of inputs.
- Reduces Code Duplication: Eliminates the need to create different method names for similar functionality.
- Supports Clean Design: Makes the code more organized and logically grouped.
- Enhances Maintainability: Easy to update or modify logic without affecting unrelated methods.
Disadvantages of Method Overloading
- Can Cause Confusion: Multiple methods with the same name may be confusing to new developers.
- Hard to Debug: Errors in choosing the correct overloaded method can be tricky to identify and fix.
- Not Always Necessary: Overuse of overloading can make the code unnecessarily complex.
- Return Type Alone Isn't Enough: You can’t overload a method just by changing the return type, which can limit flexibility.
- May Increase Maintenance Effort: If too many overloaded versions exist, maintaining them can become difficult over 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
- The Calculator class has multiple add() methods that perform different tasks, like adding integers, doubles, or concatenating strings.
- The methods are overloaded, meaning they have the same name but different numbers or types of parameters.
- In the main() method, these methods are called with appropriate inputs, and the results are displayed, like sums of numbers or a concatenated string.
- 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
In the above, we have information aboutmethod overloading and overriding in Java. Now it's time to differentiate both of them thoroughly, which will help you all understand this clearly.
Factor | Method Overloading | Method Overriding |
Definition | Having multiple methods with the same name but different parameters in the same class. | Providing a new implementation for a method already defined in the parent class. |
Purpose | To perform similar tasks in different ways (based on input types/parameters). | To change or extend the behavior of an inherited method. |
Class Involvement | Happens within the same class. | Involves a parent class and a child class. |
Method Signature | Methods must have different parameter lists (number or type of arguments). | The method signature must be the same as the parent method. |
Return Type | It can have different return types (as long as parameters differ). | It must have the same return type (or a covariant return type). |
Polymorphism Type | Compile-time polymorphism. | Run-time polymorphism. |
Annotations Used | No annotations are needed. | Uses @Override annotation (optional but recommended). |
Points to Remember
- Method overloading happens within the same class
- Method overloading happens within the same class
- Methods have the same name but different parameter lists
- Return type can be different, but not sufficient alone for overloading
- Represents compile-time (static) polymorphism
- Can also apply to constructors
- Helps improve code readability and flexibility
Method Overriding
- Happens between superclass and subclass
- Method name, parameters, and return type must be exactly the same
- Represents runtime (dynamic) polymorphism
- Requires inheritance
- The overriding method cannot reduce the visibility of the original method
- Only instance methods can be overridden (not static, constructors, or private methods)
- Use @Override annotation to avoid mistakes
Conclusion
In conclusion, overloading and overriding in Java are important ideas that make coding more powerful and flexible. Overloading in Java lets you create methods with the same name but different inputs, while overriding in Java allows a child class to change the behavior of a method from its parent class. Learning these concepts helps you write better programs and makes your code easier to use and understand.Master tech skills for free with our Free Tech Courses—perfect for beginners and experts alike! Advance your career with our Azure AI & ML Certification Training and Azure Cloud DevOps Engineer Certification Training, designed to make you job-ready.
Mostly Asked Articles: |
Java vs Python: Which Language is Better for the Future? |
Lambda Expressions in Java: Explained in Easy Steps |
HashMap in Java: A Detailed Explanation |
Q 1: Which of the following is true about method overloading in Java?
Explanation: Method overloading in Java allows multiple methods with the same name in a class, provided they differ in the number or type of their parameters.
Q 2: What is the key difference between method overloading and method overriding?
Explanation: Method overloading is a compile-time concept, while method overriding is a runtime concept because the method call is resolved using dynamic dispatch.
Q 3: Which rule must be followed in method overriding but not in method overloading?
Explanation: Method overriding requires the same method signature (name, parameter list, and return type) as the method in the parent class, which is not a requirement for overloading.
Q 4: Which of the following is true about method overriding in Java?
Explanation: Method overriding enables a subclass to provide a specific implementation of a method that is already defined in its parent class, following the same signature.
Q 5: Which of the following can be overloaded in Java?
Mostly Asked Questions on Interviews |
Top 50 Java MCQ Questions |
Top 50+ Java 8 Interview Questions & Answers 2025 |
Java Full Stack Developer Interview Questions |
FAQs
Take our Java skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.