difference between method overloading and method overriding in java

What are differences between method overloading and method overriding in java

Method overloading and method overriding are two important concepts in Java related to polymorphism. Let’s compare them in terms of their definitions, use cases, and characteristics:

difference between method overloading and method overriding in java

Method Overloading:

1. Definition: Method overloading occurs when a class has multiple methods with the same name but different parameter lists (different types or different numbers of parameters).

2. Syntax: The methods must have the same name but differ in the number or types of parameters.

3. Use Cases:

  • Providing multiple ways to perform a similar operation with different parameter sets.
  • Enhancing readability and maintainability by using the same method name for related operations.

4. Decision Time: Decided at compile time (compile-time polymorphism or static polymorphism).

Example:

Example
public class MathOperations {
    public int add(int a, int b) {
        return a + b;
    }

    public double add(double a, double b) {
        return a + b;
    }

    public String concatenate(String str1, String str2) {
        return str1 + str2;
    }
}

Usage:

Example

MathOperations math = new MathOperations();
int result1 = math.add(2, 3);           // Calls the first method
double result2 = math.add(2.5, 3.5);    // Calls the second method
String result3 = math.concatenate("Hello", "World");

Method Overriding:

1. Definition: Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass.

2. Syntax: The subclass method must have the same signature (name, return type, and parameters) as the superclass method.

3. Use Cases:

  • Modifying or extending the behavior of a method in a subclass.
  • Achieving polymorphic behavior through dynamic method dispatch.

4. Decision Time: Decided at runtime (run-time polymorphism or dynamic polymorphism).

Example:

Example
// Parent class
class Animal {
    void makeSound() {
        System.out.println("Animal makes a sound");
    }
}

// Subclass overriding the makeSound method
class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Dog barks");
    }
}

Usage:

Example

Animal myAnimal = new Dog();
myAnimal.makeSound();  // Calls the overridden method in Dog

Key Differences:

FeatureMethod OverloadingMethod Overriding
OccurrenceWithin the same class.Between a superclass and its subclass.
SyntaxDifferent parameter lists or types.Same signature (name, return type, parameters).
Decision TimeCompile time (static polymorphism).Runtime (dynamic polymorphism).
Use CasesProviding multiple versions of a method with different parameters.Modifying or extending the behavior of a method in a subclass.
Examplejava public int add(int a, int b) { //... } public double add(double a, double b) { //... }java class Animal { void makeSound() { //... } } class Dog extends Animal { @Override void makeSound() { //... } }
BindingStatic binding.Dynamic binding.
Object TypeSame class or same hierarchy.Subclass object referring to a superclass object (polymorphism).

In summary, method overloading involves multiple methods within the same class, distinguished by different parameter lists, and is resolved at compile time. Method overriding occurs in a subclass, providing a specific implementation for a method in the superclass, and is resolved at runtime. Both concepts contribute to the flexibility and versatility of object-oriented programming in Java.

Leave a Reply

Your email address will not be published. Required fields are marked *