Difference Between Method Overloading and Method Overriding in Java

 Method Overloading and Method Overriding 

Method Overloading and Method Overriding
Method Overloading and Method Overriding

Method Overloading

  • Method overloading occurs when you have multiple methods with the same name but different parameter lists.
  • This can be done in the same class.

Example:

class Calculator {
// Method to add two integers
int add(int a, int b) {
return a + b;
}

// Overloaded method to add three integers
int add(int a, int b, int c) {
return a + b + c;
}

// Overloaded method to add two double values
double add(double a, double b) {
return a + b;
}
}

public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(10, 20)); // Output: 30 (int version)
System.out.println(calc.add(10, 20, 30)); // Output: 60 (3 int version)
System.out.println(calc.add(10.5, 20.5)); // Output: 31.0 (double version)
}
}

  • Explanation: In this example, the add method is overloaded with different parameter types and counts. The compiler will choose the appropriate method at compile time based on the arguments passed.

 

Method Overriding 

  • Method overriding occurs when a subclass provides its own implementation of a method that is already defined in the superclass.

Example:

class Animal {
// Method in the parent class
void sound() {
System.out.println(“Animal sound”);
}
}

class Dog extends Animal {
// Overriding the method in the child class
@Override
void sound() {
System.out.println(“Bark”);
}
}

public class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal(); // Creating an object of the parent class
myAnimal.sound(); // Output: Animal sound

Animal myDog = new Dog(); // Creating an object of the subclass
myDog.sound(); // Output: Bark
}
}

  • Explanation: In this example, the sound method is overridden in the Dog class to provide a more specific implementation. At runtime, Java will use the Dog class’s sound method (since the object is of type Dog), even though the reference is of type Animal. This is an example of runtime polymorphism.

Method Overloading vs. Method Overriding

FeatureMethod OverloadingMethod Overriding
DefinitionMethod overloading occurs when multiple methods have the same name but differ in parameters (either in number, type, or both) within the same class.Method overriding occurs when a subclass provides a specific implementation for a method already defined in its parent class with the same method signature.
PurposeIt allows multiple methods to perform similar tasks with different types or numbers of parameters.It allows a subclass to provide a specific behavior for a method that is already defined in its superclass.
Method SignatureIn overloading, the method name is the same, but the parameters (number, type, or order) are different.In overriding, the method signature must be the same, i.e., the same name and same parameters (type, number, and order).
Return TypeThe return type can be different or the same for overloaded methods, but it doesn’t differentiate between overloaded methods.The return type must be the same or a subtype (covariant return type) as in the parent class.
Time of BindingCompile-time polymorphism (Static Binding). The method to be called is determined at compile time.Runtime polymorphism (Dynamic Binding). The method to be called is determined at runtime, depending on the object type.
InheritanceNo inheritance involved. Overloading can occur within the same class.Involves inheritance. The subclass overrides a method inherited from its parent class.
Access ModifiersOverloaded methods can have different access modifiers (public, private, protected).The access modifier in the subclass method should be the same or more permissive than the parent method’s access modifier.
Method Signature ImpactOverloading is determined based on the method signature (name + parameters).Overriding requires an exact match in method signature (name + parameters).
Examplejava class Animal { void sound() { System.out.println("Animal sound"); } void sound(String type) { System.out.println(type + " sound"); } }java class Animal { void sound() { System.out.println("Animal sound"); } } class Dog extends Animal { @Override void sound() { System.out.println("Bark"); } }
By using method overloading, you can create multiple methods with the same name but different parameters to handle different input types or amounts. On the other hand, method overriding allows subclasses to provide specific implementations of methods that are already defined in a superclass.

Leave a Comment