Overriding in Java
What is Overriding in Java?
Method overriding in Java, a powerful feature of object-oriented programming, is the process of redefining a method of superclass by the subclass. It allows you to customize a particular feature of an object of superclass, just like modifying the steering feature in a car to suit a specific driving style.
Let’s consider a practical example. We have a superclass ‘Car’ with a method ‘steering() ‘. Imagine we have a subclass ‘Ford’ that extends ‘Car’. In this scenario, the ‘steering()’ method in ‘Ford’ can override to achieve automation, which may differ from the ‘steering()’ method in ‘Car’. It’s important to note that the methods specified in the subclass are already present in the superclass, forming a hierarchical relationship.
The superclass is defined before it is in the subclass. Overriding allows the subclass to implement superclass method, whose implementation may differ from the one in the superclass.
Java Overriding:
Single inheritance, a fundamental concept in Java, plays a crucial role in method overriding. It allows a class in Java to inherit properties and methods from a single superclass, paving the way for the subclass to override the superclass’s methods with its own implementation. This is a key prerequisite for implementing method overriding in Java.
Let’s have a brief overview of the topic inheritance to enable the understanding of over-riding.
There are different types of inheritance in programming languages.
They include the following,
1. Simple Inheritance
2. Multiple Inheritance
3. Multi-level Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Note: However, Java does not support multiple inheritance.
Importantly, simple or single inheritance is mandatory when implementing method overriding.
In simple inheritance,
Class B extends class A.
When B extends (inherits) A;
The meaning of Class B extends Class A is that all properties of ‘A’ can be accessed by sub-class B.
However, one condition is that it cannot access A’s private properties.
Superclass A cannot access any of the local properties of subclass B.
Overriding Example in Java
class A
{
int x;
float y;
public void calculate(int x, float y) {
System.out.println("sum : "+(x+y));
}
}
class B extends A
{
public void calculate(int x, float y) {
System.out.println("Product : "+(x*y));
}
public void display() {
System.out.println("x Value : "+x);
System.out.println("y Value : "+y);
}
public static void main(String args[])
{
A a = new A();
a.calculate (5, 5.5f); // Super-class method
// a.display(); // Error: Super-class cannot access sub-class properties
System.out.println("----------------------");
B b = new B();
b.calculate (7, 7.5f); // Overriding method in sub class
b.display(); // No error
System.out.println("-----------------------");
A a1 = new B();
a1.calculate(9, 9.5f); // Sub-class overriding method
// a1.display(); // Error : Super-class reference cannot access sub-class properties
}
}
Polymorphism in Java
Polymorphism in Java, a fundamental and crucial concept, can be best understood through a relatable example. Imagine an object’s ability to take on many forms, just like a chameleon in the real world. A chameleon changes its color to blend with the surroundings, a strategy that helps protect itself from enemies. This simple yet powerful example makes the concept of polymorphism more accessible and less daunting.
Thus, an object can be referred to by multiple types, each with its own implementation of the object’s Behavior.
Overriding in Java: How to Achieve Polymorphism through Objects?
Through method overriding and method overloading, we can achieve polymorphism.
It is necessary to implement overriding and overloading to achieve polymorphism.
The JVM (Java Virtual Machine) calls respective methods based on the type of arguments.
What is Polymorphism in Java?
Polymorphism in Java refers to the ability of program methods to take one or more forms.
Types of Polymorphism in Java
There are two types of polymorphisms in Java.
They include,
1) Runtime Polymorphism, and
2) Compile Time Polymorphism.
Polymorphism Overloading and Overriding
While runtime polymorphism is also known as method overriding, compile-time polymorphism is known as method overloading.