Introduction:
In object-oriented programming, polymorphism is a significant concept that allows code reuse and flexibility. One way to achieve polymorphism in Java is through method overriding. Method overriding is a mechanism that allows a child class to provide its own implementation of a method that is already defined in its parent class. In this article, we will discuss the rules for method overriding in Java, why it is useful, and examples that demonstrate its usage.
Why is overriding in Java useful? Overriding is an essential aspect of polymorphism in Java because it allows the program to accept different forms of objects and treat them as the same. By defining the same method in a parent class and its child classes with different implementations, the program can perform different operations based on the object's type that triggers the method. This feature makes Java code more flexible and easier to maintain.
When is it ideal to apply overriding in Java? Overriding is ideal when dealing with related classes where the child class is a specialization of the parent class. It is also useful when you need to implement common functionality in a parent class while allowing the child class to modify or extend it. Overriding can also be used when implementing interfaces and abstract classes, where the child class is required to provide its own implementation of the methods declared in the parent class or interface.
Rules for Method Overriding in Java: To successfully override a method in Java, the following rules must be followed:
Method name and signature: The method in the child class must have the same name and signature as the method in the parent class.
Inheritance: There must be an inheritance relationship between the parent and child classes.
Access Modifiers: The access modifier for the overriding method must be the same or more permissive than the overridden method in the parent class. This means if the method in the parent class is public, the overriding method in the child class can be public or protected but not private.
Return Type: The return type of the method in the child class must be the same as or a subtype of the return type in the parent class.
Exceptions: The child class cannot declare new checked exceptions that are not present in the parent class's method signature. However, it can declare fewer or narrower checked exceptions or any unchecked exceptions.
Example 1: Method Overriding in Vehicles Let's create a hierarchy of vehicles with a parent class named Vehicle and two child classes named Bike and Car. The Vehicle class has an engine method, and we will override this method in the Bike and Car classes to provide their own implementation.
csharpclass Vehicle {
void engine() {
System.out.println("This is a vehicle engine");
}
}
class Bike extends Vehicle {
void engine() {
System.out.println("This is a bike engine");
}
}
class Car extends Vehicle {
void engine() {
System.out.println("This is a car engine");
}
}
public class Example1 {
public static void main(String[] args) {
Vehicle honda = new Bike();
honda.engine();
Vehicle benz = new Car();
benz.engine();
}
}
Output:
pythonThis is a bike engine
This is a car engine
In the example above, we created three classes: Vehicle, Bike, and Car. The Bike and Car classes extend the Vehicle class and override the engine method to provide their own implementation. In the main method, we create instances of Bike and Car classes and assign them to Vehicle objects. When we call the engine method on these objects, Java uses the overridden method in the corresponding child class.
Example 2: Method Overriding in Banks Let's create a Bank class with a getRateOfInterest method, and we will override this method in three child classes named SBI, ICICI
here's an example code for that:
rubyclass Bank:
def getRateOfInterest(self):
return 0
class SBI(Bank):
def getRateOfInterest(self):
return 7
class ICICI(Bank):
def getRateOfInterest(self):
return 8
class HDFC(Bank):
def getRateOfInterest(self):
return 9
In the above example, we have a base class named Bank
with a method named getRateOfInterest
that returns 0 by default. Then, we create three child classes SBI
, ICICI
, and HDFC
, which inherit from the Bank
class and override the getRateOfInterest
method with their own implementations.
For instance, the SBI
class overrides the getRateOfInterest
method to return 7, while the ICICI
class overrides it to return 8, and the HDFC
class overrides it to return 9.
This way, we can use the same method name across multiple classes, but each class can define its own implementation of the method. In this case, each bank can define its own interest rate without having to worry about the implementation details of the other banks.