Method overloading is a feature in Java that allows a class to have multiple methods with the same name but different parameters. When a method is called, the Java compiler determines which method to call based on the number and types of arguments passed to it. Method overloading is used to provide different ways of calling the same method with different inputs or behaviors. Here’s an example:
public class MyClass {
public void print(String message) {
System.out.println(message);
}
public void print(int number) {
System.out.println(number);
}
}
MyClass obj = new MyClass();
obj.print("Hello"); // calls the first print() method
obj.print(42); // calls the second print() method
In this example, the MyClass class has two methods with the same name (print()) but different parameters. When we call the print() method with a string argument, the first print() method is called, and when we call the print() method with an integer argument, the second print() method is called.
Method overriding, on the other hand, is a feature in Java that allows a subclass to provide a specific implementation of a method that is already provided by its superclass. When a method is called on an object of the subclass, the Java virtual machine determines which implementation of the method to call based on the actual type of the object. Here’s an example:
public class Animal {
public void makeSound() {
System.out.println("The animal makes a sound");
}
}
public class Cat extends Animal {
public void makeSound() {
System.out.println("Meow");
}
}
Animal animal = new Animal();
animal.makeSound(); // calls the makeSound() method of the Animal class
Cat cat = new Cat();
cat.makeSound(); // calls the makeSound() method of the Cat class
Animal animal2 = new Cat();
animal2.makeSound(); // calls the makeSound() method of the Cat class
In this example, the Animal class has a makeSound() method that prints a generic message. The Cat class extends the Animal class and overrides the makeSound() method with a specific implementation that prints "Meow". When we call the makeSound() method on an object of the Animal class, the makeSound() method of the Animal class is called. When we call the makeSound() method on an object of the Cat class, the makeSound() method of the Cat class is called. When we call the makeSound() method on an object of the Cat class stored in an Animal reference variable, the makeSound() method of the Cat class is still called, because the actual type of the object is Cat.
In summary, method overloading is a feature in Java that allows a class to have multiple methods with the same name but different parameters, while method overriding is a feature that allows a subclass to provide a specific implementation of a method that is already provided by its superclass. The key difference between the two is that method overloading happens in the same class, while method overriding happens in a subclass. Additionally, method overloading is determined at compile time, while method overriding is determined at runtime based on the actual type of the object.