Java Interview Questions
Java
Web DevelopmentBackendQuestion 5
Explain method overloading and method overriding.
Answer:
Method overloading and method overriding are both techniques used in Java to achieve polymorphism, but they serve different purposes and are used in different contexts.
Method Overloading
Definition: Method overloading occurs when two or more methods in the same class have the same name but different parameters (different type, number, or both). It allows a class to have more than one method with the same name, enhancing the readability of the code.
Key Points:
- Same method name.
- Different parameter list (type, number, or both).
- Can change return type.
- Compile-time polymorphism (also known as static polymorphism).
Example:
public class MathOperations {
// Method with two integer parameters
public int add(int a, int b) {
return a + b;
}
// Method with three integer parameters
public int add(int a, int b, int c) {
return a + b + c;
}
// Method with two double parameters
public double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
MathOperations math = new MathOperations();
System.out.println(math.add(5, 3)); // Output: 8
System.out.println(math.add(5, 3, 2)); // Output: 10
System.out.println(math.add(5.5, 3.2)); // Output: 8.7
}
}
Method Overriding
Definition: Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameters as the method in the superclass.
Key Points:
- Same method name.
- Same parameter list.
- Same return type (or a subtype, known as covariant return type).
- Runtime polymorphism (also known as dynamic polymorphism).
- Used for providing specific implementation in the subclass.
Example:
class Animal {
// Method to be overridden
public void makeSound() {
System.out.println("Some generic animal sound");
}
}
class Dog extends Animal {
// Overriding method
@Override
public void makeSound() {
System.out.println("Bark");
}
}
class Cat extends Animal {
// Overriding method
@Override
public void makeSound() {
System.out.println("Meow");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.makeSound(); // Output: Bark
Animal myCat = new Cat();
myCat.makeSound(); // Output: Meow
}
}
Key Differences
-
Purpose:
- Method Overloading: Increases the readability of the program by allowing multiple methods with the same name but different parameters.
- Method Overriding: Provides specific implementation in a subclass for a method already defined in the superclass.
-
Binding:
- Method Overloading: Compile-time polymorphism (binding is done at compile time).
- Method Overriding: Runtime polymorphism (binding is done at runtime).
-
Inheritance:
- Method Overloading: Can occur within the same class or in a subclass.
- Method Overriding: Must involve at least one superclass and one subclass.
-
Return Type:
- Method Overloading: Can change the return type.
- Method Overriding: Must have the same return type or a covariant return type.
-
Access Modifier:
- Method Overloading: Access modifier can be different.
- Method Overriding: Access modifier must be the same or less restrictive.
Conclusion
- Method Overloading allows multiple methods with the same name but different parameters within the same class, facilitating flexibility and readability.
- Method Overriding enables a subclass to provide a specific implementation for a method already defined in its superclass, promoting dynamic polymorphism and enabling runtime decision making.
By using method overloading and method overriding appropriately, Java developers can create flexible and maintainable code.