0% found this document useful (0 votes)
16 views10 pages

Method Overriding in Java: Updated On Aug 31, 2023 14:21 IST

Uploaded by

aravind29072005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

Method Overriding in Java: Updated On Aug 31, 2023 14:21 IST

Uploaded by

aravind29072005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Method Overriding in Java

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.

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?

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

Rules of Method Overriding in Java

How to prevent Method Overriding in Inheritance?

In Conclusion

Also read: Method Overloading in Java

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.

Explore Free Java Courses

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?

Abst ract Class in Java


The tuto rial article belo w co vers data abstractio n using abstract class in Java
with examples and explanatio ns.

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.

Super Keyword in Java


This Java Tuto rial article co vers o ne o f the mo st impo rtant co ncepts in
Inheritance which is Super Keywo rd in Java. Here, we’ll go thro ugh the uses o f
super keywo rd alo ng with...re ad m o re

Method Overriding Implementation in Java

The below example shows the implementation and usage of method overriding in
Java.

Example:

Copy code

//A Simple made up Instance to show method overriding in Java


class Waka_Waka_Song {

Waka_Waka_Song() {
Syst em.out .print ln("Song--Init ializing Waka Waka by Shakira...");
}

public void play() {


Syst em.out .print ln("Song-- Playing...");
}
}

//Spotify wants Waka Waka song on their Platform


class Spot if y ext ends Waka_Waka_Song {

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...");
}

public void play() {


Syst em.out .print ln("Spot if y-- Playing.. ");
}

//Amazon_Music wants Waka Waka song on their Platform

class Amazon_Music ext ends Waka_Waka_Song {

Amazon_Music() {
Syst em.out .print ln("Amazon Music...");
}
public void play() {
Syst em.out .print ln("Amazon Prime-- Playing...");
}
}

//A User Playing songs on Spotify and then Amazon Music


class User {
public st at ic void main(St ring[] args) {

Syst em.out .print ln("Playing Waka Waka on Spot if y... ");


Spot if y s = new Spot if y();
s.play();

Syst em.out .print ln("


Playing Waka Waka on Amazon Music");

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:

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.

T here should be superclass and subclass implementing methods overriding with the same
name and parameters.

T he subclass method to be overridden should not be declared as f inal or static.

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.

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

Opt ion 1: Using Final OR St at ic Keyword t o prot ect it f rom get t ing overridden

Example:

Copy code

class Quadrilat eral {

//Display() method in class Quadrilateral (overridden method)


public f inal void display()
{
Syst em.out .print ln("I am a Quadrilat eral");
}
}

class Rect angle ext ends Quadrilat eral {

//Display() method in class Rectangle (overriding method)


public void display()
{

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");
}

public void print Message()


{
// this calls subclass method (overridding method)
display();
}
}

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:

Read: Exception Handling in Java.

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

lass Quadrilat eral {

//Display() method in class Quadrilateral (overridden method)


public void display(){
Syst em.out .print ln("I am a Quadrilat eral (a Superclass)");
}
}

class Rect angle ext ends Quadrilat eral {

//Display() method in class Rectangle (overriding method)


public void display(){
Syst em.out .print ln("I am a Rect angle (a subclass)");
}

public void print Message(){

// this calls subclass method (overridding method)


display();

// this calls superclass method (overridden method)


super.display();

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:

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 dif f erence between Overloading and 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.
Why do we need method overriding?

How can I protect a method f rom getting overridden by its subclass?

How can I access a superclass overridden method?

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.

You might also like