0% found this document useful (0 votes)
2 views2 pages

Overriding in Java

Overriding in Java allows a subclass to provide a specific implementation of a method already defined in its superclass. This feature enables run-time polymorphism, where the method executed is determined by the object's type rather than the reference type. An example illustrates how a method in a child class can override a method in the parent class, demonstrating this concept in action.

Uploaded by

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

Overriding in Java

Overriding in Java allows a subclass to provide a specific implementation of a method already defined in its superclass. This feature enables run-time polymorphism, where the method executed is determined by the object's type rather than the reference type. An example illustrates how a method in a child class can override a method in the parent class, demonstrating this concept in action.

Uploaded by

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

Overriding in Java

In Java, Overriding is a feature that allows a subclass or child class to provide a specific
implementation of a method that is already provided by one of its super-classes or parent
classes. When a method in a subclass has the same name, the same parameters or
signature, and the same return type(or sub-type) as a method in its super-class, then the
method in the subclass is said to override the method in the super-class.

Method overriding is one of the ways by which Java achieves Run Time Polymorphism .
The version of a method that is executed will be determined by the object that is used to
invoke it. If an object of a parent class is used to invoke the method, then the version in
the parent class will be executed, but if an object of the subclass is used to invoke the
method, then the version in the child class will be executed. In other words, it is the type
of the object being referred to (not the type of the reference variable) that determines
which version of an overridden method will be executed.

Example of Method Overriding in Java


Below is the implementation of the Java Method Overriding:

// Java program to demonstrate


// method overriding in java

// Base Class
class Parent {
void show()
{ System.out.println("Parent's show()"); }
}

// Inherited class
class Child extends Parent {
// This method overrides show() of
Parent
@Override void show()
{
System.out.println("Child's
show()");
}
}

// Driver class
class Main {
public static void main(String[] args)
{
// If a Parent type reference refers
// to a Parent object, then Parent's
// show is called
Parent obj1 = new Parent();
obj1.show();

// If a Parent type reference refers


// to a Child object Child's show()
// is called. This is called RUN
TIME
// POLYMORPHISM.
Parent obj2 = new Child();
obj2.show();
}
}
Output
Parent's show()
Child's show()

You might also like