Differentiate between method overriding and overloading.


Q.) Differentiate between method overriding and overloading.

Subject: Object Oriented Programming and Methodology

Method Overriding:

  • Definition: Method overriding occurs when a subclass defines a method with the same name and signature as a method in its superclass.
  • Purpose: Overriding allows subclasses to provide their own implementation of a method that is inherited from a superclass.
  • Syntax: In Java, method overriding is achieved by declaring a method in a subclass with the same name and signature as a method in its superclass. The method in the subclass must have the same return type and parameter types as the method in the superclass.
  • Example:
class Animal {
    public void makeSound() {
        System.out.println("Animal makes a sound.");
    }
}

class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Dog barks.");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal animal = new Animal();
        animal.makeSound(); // Outputs "Animal makes a sound."

        Dog dog = new Dog();
        dog.makeSound(); // Outputs "Dog barks."
    }
}

Method Overloading:

  • Definition: Method overloading occurs when a class defines multiple methods with the same name but different parameters.
  • Purpose: Overloading allows a class to provide multiple implementations of a method, each with different functionality.
  • Syntax: In Java, method overloading is achieved by declaring multiple methods with the same name in a class, but with different parameter types.
  • Example:
class Calculator {
    public int add(int a, int b) {
        return a + b;
    }

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

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

    public static void main(String[] args) {
        Calculator calculator = new Calculator();

        int result1 = calculator.add(1, 2); // Outputs 3
        double result2 = calculator.add(1.5, 2.5); // Outputs 4.0
        String result3 = calculator.add("Hello", "World"); // Outputs "HelloWorld"
    }
}

Differences between Method Overriding and Overloading:

Feature Method Overriding Method Overloading
Definition A subclass defines a method with the same name and signature as a method in its superclass A class defines multiple methods with the same name but different parameters
Purpose Allows subclasses to provide their own implementation of a method inherited from a superclass Allows a class to provide multiple implementations of a method, each with different functionality
Syntax Declared in a subclass with the same name and signature as a method in its superclass Declared in a class with the same name but different parameter types
Example class Dog extends Animal { @Override public void makeSound() { System.out.println("Dog barks."); } } class Calculator { public int add(int a, int b) { ... } public double add(double a, double b) { ... } }