Overriding in Java
Overriding in Java
Method overriding is one of the way by which java achieve 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.
// Base Class
class Parent {
void show()
{
System.out.println("Parent's show()");
}
// Inherited class
@Override
void show()
{
System.out.println("Child's show()");
}
}
// Driver class
class Main {
{
obj1.show();
// POLYMORPHISM.
obj2.show();
}
Output:
Parent's show()
Child's show()
class Parent {
{
}
{
}
}
{
}
@Override
{
}
// Driver class
class Main {
{
obj1.m2();
obj2.m2();
}
2. Output:
3. From parent m2()
4. From child m2()
5.
6. Final methods can not be overridden : If we don’t want a method to be
overridden, we declare it as final. Please see Using final with Inheritance .
class Parent {
void show() {}
7. Output:
8. 13: error: show() in Child cannot override show() in Parent
9. void show() { }
10. ^
11. overridden method is final
12. Static methods can not be overridden(Method Overriding vs Method
Hiding) : When you define a static method with same signature as a static method in
base class, it is known as method hiding.
The following table summarizes what happens when you define a method with the
same signature as a method in a super-class.
Superclass Instance Method Superclass Static Method
Subclass Instance Generates a compile-time
Method Overrides error
Generates a compile-time
Subclass Static Method error Hides
// overriding, it is hiding
class Parent {
{
}
void m2()
{
{
}
@Override
{
}
// Driver class
class Main {
{
obj1.m1();
obj1.m2();
}
Output:
From parent static m1()
From child non-static(instance) m2()
13. Private methods can not be overridden : Private methods cannot be overridden
as they are bonded during compile time. Therefore we can’t even override private
methods in a subclass.(See this for details).
14. The overriding method must have same return type (or subtype) : From Java
5.0 onwards it is possible to have different return type for a overriding method in
child class, but child’s return type should be sub-type of parent’s return type. This
phenomena is known as covariant return type.
15. Invoking overridden method from sub-class : We can call parent class method
in overriding method using super keyword.
// Base Class
class Parent {
void show()
{
System.out.println("Parent's show()");
}
// Inherited class
class Child extends Parent {
@Override
void show()
{
super.show();
System.out.println("Child's show()");
}
// Driver class
class Main {
{
obj.show();
}
16. Output:
17. Parent's show()
18. Child's show()
19. Overriding and constructor : We can not override constructor as parent and
child class can never have constructor with same name(Constructor name must
always be same as Class name).
20. Overriding and Exception-Handling : Below are two rules to note when
overriding methods related to exception-handling.
Rule#1 : If the super-class overridden method does not throw an exception,
subclass overriding method can only throws the unchecked exception, throwing
checked exception will lead to compile-time error.
*/
class Parent {
void m1()
{
}
void m2()
{
}
}
@Override
{
}
@Override
{
}
Output:
error: m2() in Child cannot override m2() in Parent
void m2() throws Exception{ System.out.println("From child
m2");}
^
overridden method does not throw Exception
Rule#2 : If the super-class overridden method does throws an exception,
subclass overriding method can only throw same, subclass exception. Throwing
parent exception in Exception hierarchy will lead to compile time error.Also there
is no issue if subclass overridden method is not throwing any exception.
class Parent {
{
}
@Override
{
}
}
@Override
{
}
@Override
void m1()
{
}
@Override
{
}
Output:
error: m1() in Child4 cannot override m1() in Parent
void m1() throws Exception
^
overridden method does not throw Exception
21.
22. Overriding and abstract method: Abstract methods in an interface or abstract
class are meant to be overridden in derived concrete classes otherwise a compile-time
error will be thrown.
23. Overriding and synchronized/strictfp method : The presence of
synchronized/strictfp modifier with method have no effect on the rules of overriding,
i.e. it’s possible that a synchronized/strictfp method can override a non
synchronized/strictfp one and vice-versa.
Note :
1. In C++, we need virtual keyword to achieve overriding or Run Time
Polymorphism. In Java, methods are virtual by default.
2. We can have multilevel method-overriding.
// multi-level overriding
// Base Class
class Parent {
void show()
{
System.out.println("Parent's show()");
}
// Inherited class
// Inherited class
void show()
{
System.out.println("GrandChild's show()");
}
// Driver class
class Main {
{
obj1.show();
}
3. Output:
4. GrandChild's show()
5. Overriding vs Overloading :
// of overriding in Java
// Base Class
class Employee {
int salary()
{
return base;
}
// Inherited class
int salary()
{
}
// Inherited class
int salary()
{
}
// Driver class
class Main {
{
System.out.println(e.salary());
}
printSalary(obj1);
printSalary(obj2);
}
Output:
Manager's salary : 30000
Clerk's salary : 20000
Related Article:
Dynamic Method Dispatch or Runtime Polymorphism in Java
Overriding equals() method of Object class
Overriding toString() method of Object class
Overloading in java
Output of Java program | Set 18 (Overriding)
This article is contributed by Twinkle Tyagi and Gaurav Miglani. If you like
GeeksforGeeks and would like to contribute, you can also write an article
using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org.
See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more
information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java
Foundation and Collections concepts with the Fundamentals of Java and Java
Collections Course at a student-friendly price and become industry ready. To complete
your preparation from learning a language to DS Algo and many more, please
refer Complete Interview Preparation Course.