Updated March 17, 2023
Introduction to Overriding in Java
- Java has been a very well-known general-purpose language that is object-oriented, class-based in nature, along with primitive data types, which are also known to contain few implementation related dependencies, which make the working of the application developers very easy as it provides them with ease of write once and read anywhere. Before hopping onto the concept of overriding in detail, one should be thorough with the concept of the parent class as well as child class.
- The parent class is the superclass, i.e. the class which resides on the top and has a unique set of individual characteristics and entities, whereas the child class or the base class is the one that is present on the lower end of the parent class and has a different yet resembling set of features. Also, Overriding in Java extends itself to get the parent class features by an IS-A relationship and therefore is termed as an inheritance.
- One such feature of this programming language is the Overriding feature, according to which in any object-oriented programming language, overriding becomes a feature that makes use of a subclass or a child class in order to provide a particular implementation of a program’s method which is already in place by one of the parent classes or the superclasses. For example, if the subclass method has the same name, same signature or parameter and similar return type or a subtype which is used as a method in the superclass, then the subclass method overrides the superclass method.
- In other words, if a child class or a base class or a subclass gets the same method name which is of the parent method, then it is known as method overriding in Java. To put it better, if a subclass has been providing method implementations that are already declared by any of the parent classes, it can also be known to be method overriding in Java.
The main usages of Java method overriding are:
- It is used to provide a particular implementation of a method that its corresponding superclass has already provided.
- The concept of method overriding also extends to runtime polymorphism.
How Overriding works in Java?
In the case of method overriding, whenever any method in the subclass has the same name as that of its parent method, and the call from the runtime is generated as such that it points to the method name, which is common to both and one of them executes is when method overriding is said to exist. The main challenge while doing this is when the same method name is used for both the parent and child classes, and at runtime, the call is provided in such a way that it has to correspond to either the child class or the parent class. The compiler of the language then decides this by taking into account the number of parameters, types of parameters, etc. Based upon which the function name to be called is determined. Method overloading and method overriding have been crucial concepts in the field of core java. It holds significance as the compiler needs to be clear as to which method to call.
Examples of Overriding in Java
This feature can exist whenever a function name has the same name as the parent class name, especially at the run time mechanism. Here is a simple example to explain this concept in detail. Firstly we will be discussing the problem without overriding it and next why it came into existence.4.
First Example:
class MyVehicle{
void run(){
System.out.println("It's a running vehicle");}
}
class MyCar extends MyVehicle{
public static void main(String args[]){
MyCar obj = new MyCar();
obj.run();
}
}
Output:
The output is: It’s a running vehicle. I had to provide a specific implementation of the method run() provided in the subclass. Therefore, we could use the method overriding feature in the future.
Second Example:
class MyVehicle{
void run(){System.out.println("My vehicle is running");}
} class MyCar2 extends MyVehicle{
void run(){System.out.println("My car is running");}
public static void main(String args[]){
MyCar2 obj = new MyCar2();
obj.run();
}
}
Output:
My Car is running is the output of the above problem example.
Explanation: Now, if you look closely at what happened in both the examples, the first example talks about extending the child class with the parent class, which is also the case in the second case. But in this example, the MyCar2 extends MyVehicle, and as defined by the definition of method overriding, the decision to the call is made at runtime, i.e. at the time run() method was called. Therefore, when this method was called, the call first went to the child class or the base class as it was already extending all the properties of the parent class and therefore would be fully sufficient. Then, once it reached the base class section and ensured that the inheritance is an IS-A relationship along with the keyword extends, it successfully printed out the output: My car is running.
Rules of Method Overriding in Java
- The name of the method should be the same for both parents as well as child class.
- The parameter of the base class should be the same as that of the parent class.
- The relationship must be an IS-A relationship between the child class as well as the parent class.
Conclusion
Java has been a very old programming language, and today, it is almost used in every sphere of technology. Therefore, one has to be strongly aware of the types and concepts used in this robust programming language. The language of Java is typically divided into two segments, I.e. Core Java and Advanced Java. The concept of method overriding forms a part of Core Java where it makes use of the features of object-oriented programming techniques such as Inheritance. This is a very important topic which you should definitely understand in detail if you are looking to work in this language. I hope you liked our article. Stay with us for more articles like these.
Recommended Articles
This is a guide to Overriding in Java. Here we discuss the Introduction to Overriding in Java, Types of Overriding in Java and Rules of Method overriding in Java. You can also go through our other suggested articles to learn more–