Computer Science Related Others Courses AvailableThe Best Codder.blogspot.com

Difference between method overloading and method overriding in java

Difference-between-method-overloading-and-method-overriding-java

 

Difference between method overloading and method overriding in java

Method overloading and method overriding are two important concepts in object-oriented programming, particularly in Java.

Method overloading refers to defining multiple methods with the same name in a class, but with different parameters. These methods can have different numbers or types of parameters, but must have the same method name. During compile-time, the compiler determines which method to call based on the number, order, and types of arguments passed to it.

Example:

java
Copy code
public class Calculator {
   public int add(int x, int y) {
      return x + y;
   }
   public double add(double x, double y) {
      return x + y;
   }
}
Method overriding, on the other hand, refers to the ability of a subclass to provide its own implementation for a method that is already defined in its superclass. The overridden method must have the same name, return type, and parameters as the original method in the superclass. The purpose of method overriding is to provide a specialized implementation of the method in the subclass, while still maintaining the same method signature.

Example:

java
Copy code
public class Animal {
   public void makeSound() {
      System.out.println("Animal is making a sound");
   }
}
public class Dog extends Animal {
   public void makeSound() {
      System.out.println("Dog is barking");
   }
}
In summary, the main difference between method overloading and method overriding is that method overloading allows multiple methods to have the same name, but different parameters, while method overriding allows a subclass to provide its own implementation of a method that is already defined in its superclass.

There are many differences between method overloading and method overriding in java. A list of differences between method overloading and method overriding are given below:

No.Method OverloadingMethod Overriding
1)Method overloading is used to increase the readability of the program.Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
2)Method overloading is performed within class.Method overriding occurs in two classes that have IS-A (inheritance) relationship.
3)In case of method overloading, parameter must be different.In case of method overriding, parameter must be same.
4)Method overloading is the example of compile time polymorphism.Method overriding is the example of run time polymorphism.
5)In java, method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter.

Return type must be same or covariant in method overriding.

Post a Comment

© JAVA. The Best Codder All rights reserved. Distributed by