Overriding in Java


Click Here And Get This Posted To YOU In PDF Format

Overriding in Java

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 an 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 which 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 features of the parent class 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 which 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. 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. Or to put it better if a subclass has been providing method implementations that are already declared by any of the parent classes, then 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 has already been provided by its corresponding superclass.
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 one of the crucial concepts in the field of core java and it holds significance as the compiler needs to be clear as to which method actually to call.

Types of Overriding in Java

Whenever a function name has the same name as that of the parent class name then this feature can be said to exist especially at run time mechanism. Here is a simple example to explain this concept in detail. Firstly we will be discussing the problem without overriding 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:

first output

The output is: It’s a running vehicle. Now, I had to provide a specific implementation of the method run() which is provided in the subclass and therefore we could make use of 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:

second 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. 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 the concepts being 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 the 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. 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–

JavaScript Objects
Arrays Methods in JavaScript
Java Annotations
Overriding in OOPs

The post Overriding in Java appeared first on EDUCBA.

Read more: educba.com

What's Your Reaction?

Cry Cry
0
Cry
Cute Cute
0
Cute
Damn Damn
0
Damn
Dislike Dislike
0
Dislike
Like Like
0
Like
Lol Lol
0
Lol
Love Love
0
Love
Win Win
0
Win
WTF WTF
0
WTF

Comments 0

Your email address will not be published. Required fields are marked *

Overriding in Java

log in

Become a part of our community!

Captcha!
Don't have an account?
sign up

reset password

Back to
log in

sign up

Join BoomBox Community

Captcha!
Back to
log in
Choose A Format
Personality quiz
Trivia quiz
Poll
Story
List
Open List
Ranked List
Meme
Video
Audio
Image