Overloading and Overriding are two important concepts in object-oriented programming in Java , but they serve different purposes and behave differently.
1.Overloading (Compile-Time Polymorphism)
Definition:
Overloading occurs when multiple methods in the same class share the same name but differ in their parameters (type, number, or both). It is also known as compile-time polymorphism or static polymorphism because the method call is resolved at compile time.
Example:
Output:
Advantages:
- Increases readability by using the same method name for similar functionalities.
- Promotes code reusability by allowing different operations using the same method name with different parameters.
Uses:
Method overloading is used when functions logically represent similar operations but require different input parameters, such as:
- System.out.println() (different data types: int, char, String).
- Mathematical operations that need to handle different data types (e.g., add, subtract methods with int, double, float).
2.Overriding (Run-Time Polymorphism)
Definition:
Overriding occurs when a subclass provides a specific implementation for a method that is already defined in its parent class. The method in the subclass must have the same name, return type, and parameters as the one in the parent class. It is also known as run-time polymorphism or dynamic polymorphism because the method call is resolved at runtime.
Example:
Output:
Advantages:
- Achieves run-time polymorphism, allowing the program to decide which method to invoke based on the actual object, not its reference type.
- Helps implement specific behavior for subclasses while keeping a consistent method signature with the parent class.
Uses:
Overriding is commonly used in inheritance to define a specific behavior in a subclass, such as:
- In a graphical user interface (GUI) framework, a parent class may define a draw() method, and subclasses such as Circle, Rectangle, or Triangle may override it to provide their specific drawing behavior.
- In frameworks and libraries where methods are intended to be overridden by the user of the framework (e.g., Servlet lifecycle methods).