Method Overriding in Java
The below tutorial article covers method overriding in Java with examples. It also covers why we override the methods and how we can protect overridden if needed. Method overriding has a lot of real-life implementations and usage. So let’s begin the learning with the basics first.
Method overriding is a powerful feature in object-oriented programming that allows a subclass to provide a different implementation of a method that is already defined in its superclass. It enables the subclass to redefine the behavior of the inherited method to suit its specific requirements. Method overriding plays a crucial role in achieving polymorphism and code reuse in Java.
Contents
- What is Method Overriding in Java?
- Method Overriding Implementation in Java
- Rules of Method Overriding in Java
- How to prevent Method Overriding in Inheritance?
- In Conclusion
Also read: Method Overloading in Java
Best-suited Java courses for you
Learn Java with these high-rated online courses
What is Method Overriding in Java?
Method Overriding in Java undergoes the concepts of Inheritance and Runtime or Dynamic Polymorphism in Java. FYI, inheritance in the OOPs concept depicts the relationship between parent class (superclass) and child class (subclass). Here, the child class inherits the data member and methods from the parent class. Another concept covered here will be Dynamic Polymorphism which resolves the program at runtime.
For more, Read: OOPs Concepts in Java.
Method Overriding is redefining a superclass method with the same name, data types, and parameters in a subclass. This is also the answer to the question of why we override the method? Simple, because here, we can redefine the behavior of the method with the same name and parameters in the inherited class (or subclass) without the need to create different methods with similar functionalities.
Explore: Constructors in Java
Let’s see how to implement method overriding in Java?
Method Overriding Implementation in Java
The below example shows the implementation and usage of method overriding in Java.
Example:
//A Simple made up Instance to show method overriding in Javaclass Waka_Waka_Song { Waka_Waka_Song() { System.out.println("Song--Initializing Waka Waka by Shakira..."); } public void play() { System.out.println("Song-- Playing..."); }} //Spotify wants Waka Waka song on their Platformclass Spotify extends Waka_Waka_Song { Spotify() { System.out.println("Spotify..."); } public void play() { System.out.println("Spotify-- Playing.. "); } } //Amazon_Music wants Waka Waka song on their Platform class Amazon_Music extends Waka_Waka_Song { Amazon_Music() { System.out.println("Amazon Music..."); } public void play() { System.out.println("Amazon Prime-- Playing..."); }} //A User Playing songs on Spotify and then Amazon Music class User { public static void main(String[] args) { System.out.println("Playing Waka Waka on Spotify... "); Spotify s = new Spotify(); s.play(); System.out.println("\nPlaying Waka Waka on Amazon Music"); Amazon_Music p = new Amazon_Music(); p.play(); }}
Explanation:
Here, method play() of superclass waka_waka_song is overridden by subclasses Spotify and Amazon Music.
Output:
Read: Access Modifiers in Java
Also Read: Difference Between Overloading and Overriding in Java
Rules of Method Overriding in Java
There are a few rules to demonstrate and implement method overriding in Java. Let’s see what they are.
- There should be superclass and subclass implementing methods overriding with the same name and parameters.
- The subclass method to be overridden should not be declared as final or static.
- Always override the abstract class of the superclass.
Also Read: Difference between JDK, JRE, and JVM
How to prevent Method Overriding in Inheritance?
If we must protect the superclass method from getting overridden by the subclass, we can declare the method with the final keyword in Java. Another option to do the same would be using static keywords in Java. Hence, using final and static keywords can help the method not get overridden by subclass same name method.
Also, Read: Java Operators Explained
For more, Read: 8 Most Important Data Structures a Programmer Must Know
Option 1: Using Final OR Static Keyword to protect it from getting overridden
Example:
class Quadrilateral { //Display() method in class Quadrilateral (overridden method) public final void display() { System.out.println("I am a Quadrilateral"); }} class Rectangle extends Quadrilateral { //Display() method in class Rectangle (overriding method) public void display() { System.out.println("I am a Rectangle"); } public void printMessage() { // this calls subclass method (overridding method) display(); }} class Main { public static void main(String[] args) { Rectangle r = new Rectangle(); r.printMessage(); }}
Explanation:
Here, we have two methods with name display() in the superclass (Quadrilateral) and subclass (Rectangle), respectively. Using the final keyword with display() method in the superclass Quadrilateral restricts the accessibility to override it by the subclass Rectangle.
Output:
Read: Exception Handling in Java.
Option 2: Using Super Keyword to access the overridden method of the superclass
Another way to access the superclass overridden method is using the super keyword in Java. Let’s see how you can access the superclass overridden method in the subclass using super.
Example:
lass Quadrilateral { //Display() method in class Quadrilateral (overridden method) public void display(){ System.out.println("I am a Quadrilateral (a Superclass)"); }} class Rectangle extends Quadrilateral { //Display() method in class Rectangle (overriding method) public void display(){ System.out.println("I am a Rectangle (a subclass)"); } public void printMessage(){ // this calls subclass method (overridding method) display(); // this calls superclass method (overridden method) super.display(); }} class Main { public static void main(String[] args) { Rectangle r = new Rectangle(); r.printMessage(); }}
Explanation:
To help the Quadrilateral display() method access without getting shadowed (or overridden) by class Rectangle, we use super.display(). Hence, display() calls the method inside the class Rectangle (subclass) and super.display() calls the method of class Quadrilateral (superclass.)
Output:
Read: Implementing Array in Java
Conclusion
Hope you understood and got to learn something new by reading the above article on method overriding in Java. For more content on Java programming, stay tuned to the tutorial. And if you have any queries, feel free to share them on the link below. Till then, keep learning!
FAQs
What is the difference between Overloading and Overriding?
Method Overloading has two or more methods in a class with the same name but different parameters. In contrast, Method Overriding is redefining a superclass method with the same name, type, and parameters in a subclass.
Why do we need method overriding?
Using method overriding, we can redefine the method behavior with the same name and parameters in the inherited class (or subclass) without the need to create different methods with similar functionalities.u00a0
How can I protect a method from getting overridden by its subclass?
Using a static or final keyword with the superclass method, you can protect that method from getting overridden by the subclass.
How can I access a superclass overridden method?
Using a super keyword with the overridden method name, you can access the superclass method.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio