Method Overriding in Java: Updated On Aug 31, 2023 14:21 IST
Method Overriding in Java: Updated On Aug 31, 2023 14:21 IST
Shiksha Online
Updated on Aug 31, 2023 14:21 IST
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.
Contents
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Method Overriding Implementation in Java
In Conclusion
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.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Int erf ace in Java Explained
The belo w Java Tuto rial Article explains the interface in Java with examples.
Mo reo ver it co vers multiple inheritance thro ugh interfaces using examples.
The below example shows the implementation and usage of method overriding in
Java.
Example:
Copy code
Waka_Waka_Song() {
Syst em.out .print ln("Song--Init ializing Waka Waka by Shakira...");
}
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
class Spot if y ext ends Waka_Waka_Song {
Spot if y() {
Syst em.out .print ln("Spot if y...");
}
Amazon_Music() {
Syst em.out .print ln("Amazon Music...");
}
public void play() {
Syst em.out .print ln("Amazon Prime-- Playing...");
}
}
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Amazon_Music p = new Amazon_Music();
p.play();
}
}
Explanation:
Output:
There are a few rules to demonstrate and implement method overriding in Java.
Let’s see what they are.
T here should be superclass and subclass implementing methods overriding with the same
name and parameters.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Always override the abstract class of the superclass.
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.
For more, Read: 8 Most Important Data Structures a Programmer Must Know
Opt ion 1: Using Final OR St at ic Keyword t o prot ect it f rom get t ing overridden
Example:
Copy code
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Syst em.out .print ln("I am a Rect angle");
}
class Main {
public st at ic void main(St ring[] args)
{
Rect angle r = new Rect angle();
r.print Message();
}
}
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:
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Opt ion 2: Using Super Keyword t o access t he overridden met hod of t he
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:
Copy code
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
}
}
class Main {
public st at ic void main(St ring[] args) {
Rect angle r = new Rect angle();
r.print Message();
}
}
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:
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
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Why do we need method overriding?
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.