11.12. Structure of Classes and Objects in Java: Method Overloading and Overriding
Java, being an object-oriented programming language, offers a robust framework for defining classes and objects. A fundamental concept in this context is that of methods, which are essentially the actions that objects can perform. The flexibility of using methods is increased by two important concepts: method overloading and overriding. Let's explore each of these concepts in detail.
Method Overloading
Method overloading is a feature that allows a programmer to define multiple methods with the same name within a class, as long as they have different parameter lists. Overloading is useful when you want to perform the same operation in different ways depending on the arguments passed to the method.
public class Calculator {
// Overloading with two parameters
public int add(int a, int b) {
return a + b;
}
// Overloading with three parameters
public int add(int a, int b, int c) {
return a + b + c;
}
}
In the example above, the Calculator
class has two methods called add
. One of them adds two numbers, while the other adds three. The Java compiler distinguishes which method to call based on the arguments passed in the method call.
Method Overriding
Method overriding, on the other hand, occurs when a subclass redefines a method of the superclass. The overridden method in the subclass must have the same signature, that is, the same name, return type and parameters as the method in the superclass. Overriding is used to provide a specific implementation of a method that is already provided by its parent class.
class Animal {
public void emitSound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal {
// Override of the emitSound method
@Override
public void emitSound() {
System.out.println("The dog barks");
}
}
In the example above, the Dog
class overrides the EmitSound
method of the Animal
class. When an object of class Dog
calls EmitSound
, the new implementation is executed, not that of class Animal
.
Rules for Method Overriding
There are some rules that need to be followed to override a method correctly:
- The method in the subclass must have the same signature as the method in the superclass.
- The method in the subclass must be at least as accessible as the method in the superclass.
- The method in the subclass cannot throw wider checked exceptions than the method in the superclass.
- If the method in the superclass returns a type, the method in the subclass must return the same type or a subtype (covariance).
Differences between Overloading and Overwriting
Although both concepts allow the use of methods with the same name, they are used in different contexts and have different rules:
- Overloading occurs within the same class and is used for methods that perform the same operation in different ways, with different parameters.
- Superscript occurs between a superclass and a subclass and is used to provide a specific implementation of a method that already exists in the superclass.
Keyword super
When we override a method, we sometimes want to call the superclass's version of the method before adding subclass-specific functionality. This can be done using the super
keyword.
class Cat extends Animal {
@Override
public void emitSound() {
super.emitSound(); // Call the superclass method
System.out.println("The cat meows");
}
}
In this example, the Cat
class overrides the Sound
method, but also calls the Animal
class's implementation of the method before adding its own functionality.
Conclusion
Understanding method overloading and overriding is essential to effectively using object-oriented programming in Java. These concepts allow programmers to write more flexible and reusable code, making systems easier to maintain and expand. By mastering overloading and overwriting, you will be able to create classes that behave polymorphically, responding differently to common messages, which is a fundamental principle of object orientation.