Method Overloading vs Method Overriding
π Method Overloading
Having multiple methods with the same name but different parameters (type/number/order).
class Calculator {
int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
double add(double a, double b) { return a + b; }
}
π§ Why use it?
- It is a direct implementation of compile-time polymorphism.
- Keeps the code cohesive by logically grouping related operations under one method name.
- Improves readability and usability β clients donβt have to remember different method names.
- Enables type-safe flexibility for different argument combinations.
β Overloading enhances flexibility and maintains a clean API without sacrificing type safety β a must for robust method design.
π Method Overriding
Subclass provides a new implementation of a method inherited from its superclass.
class Animal {
void sound() {
System.out.println("Generic sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Bark");
}
}
π§ Why use it?
- It is the foundation of runtime polymorphism β actual method execution depends on the object type.
- Enables subclasses to customize behavior while keeping the interface consistent.
- Supports extensibility β you can introduce new behaviors in derived classes without touching base code.
- Aligns with the Liskov Substitution Principle, promoting interchangeable components.
β Overriding allows dynamic behavior changes, making code more adaptable and maintainable in large-scale systems.
Sure! Here’s a well-structured homework assignment for Method Overloading vs Method Overriding. It encourages hands-on practice and reinforces conceptual clarity through comparison.
π Homework: Method Overloading vs Method Overriding
π― Objective:
To help students understand the difference between method overloading (compile-time polymorphism) and method overriding (runtime polymorphism) by implementing both in practical Java classes.
π Tasks
π§ͺ Part 1: Method Overloading
Create a class named
MathUtils
with the following overloaded methods:int multiply(int a, int b)
double multiply(double a, double b)
int multiply(int a, int b, int c)
In the
main()
method, call eachmultiply
method with appropriate parameters and print the results.Write a brief comment in your code explaining why overloading improves API design and user experience.
π§ͺ Part 2: Method Overriding
Create a parent class named
Shape
with a method:void draw() { System.out.println("Drawing a shape..."); }
Create two child classes:
Circle
that overridesdraw()
to print"Drawing a circle..."
.Square
that overridesdraw()
to print"Drawing a square..."
.
In the
main()
method:- Declare a
Shape
reference and assign it tonew Circle()
and calldraw()
. - Repeat with
new Square()
.
- Declare a
Add a comment explaining how and why overriding supports runtime polymorphism.
π¬ Bonus Discussion (Optional):
- What would happen if the
@Override
annotation is removed? - Can we overload methods by only changing the return type? Why or why not?
β Submission Requirements:
- Java file with all classes and
main()
method. - Clear, well-commented code explaining both overloading and overriding.
- Your explanation of why these concepts are essential to polymorphism.
Let me know if youβd like me to generate a sample solution for this homework too!
Would you like me to move to the next core Java topic? Options:
- Access Modifiers
- Static Keyword
- Interfaces and Abstract Classes
- Final Keyword