Type of Method Overriding
Concept of Method Overriding is very important topic for tha progammer.
Child class can redefine methods of the parent class. Method Signature i.e return type, parameter type and number of parameters is kept as defined in the parent class. Method overriding is done to achieve Run Time Polymorphism (An overridden method is called according to the object invoking it, not according to the reference type). Method overriding is dynamic binding.
package code.mindclues.overriding;
//A Simple Java program to overriding in Java
//Base Class
class Parent{
void show() { System.out.println("Parent's class show()"); }
}
//Inherited class
class Child extends Parent{
// This method overrides show() of Parent
void show() { System.out.println("Child's class 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 class show()
Child's class show()
Some Importants Facts:
1- Multilevel overriding in java.
// A Simple Java program to demonstrate multi-level overriding in Java Base Class
class Parent{
void show() { System.out.println( "Parent's class method show()" ); }
}
// Inherited class
class Child extends Parent{
// This method overrides show() of Parent
void show() { System.out.println( "Child's class method show()" ); }
}
// Inherited class
class GrandChild extends Child{
// This method overrides show() of Parent
void show() { System.out.println( "GrandChild's class method show()" ); }
}
// Driver class
class Main{
public static void main(String[] args) {
Parent obj1 = new GrandChild();
obj1.show();
}
}
|
Output :
GrandChild's class method show()
2 - If we don’t want a method to be overridden, we declare it as final.
// A Simple Java program to demonstrate working of final and overriding in Java
class Parent{
// Can't be overridden
final void show() { }
}
class Child extends Parent{
// This would produce error
void show() { }
}
|
Output :
13: error: show() in Child cannot override show() in Parent
void show() { }
^
overridden method is final
3- In overridden method, we can not give weaker access, but reverse is possible. For example, a protected method in base class cannot be overridden as private in derived class.
// This program won't compile as we have given weaker access in derived class
class Parent{
// Can't be overridden
protected void show() { }
}
class Child extends Parent{
// This would produce error. If we replace private with protected or public, the program would work.
private void show() { }
}
|
Output :
12: error: show() in Child cannot override show() in Parent
private void show() { }
^
attempting to assign weaker access privileges; was protected
1 error
4- Private methods cannot be overridden as they are bonded during compile time. Therefore we can’t even override private methods in an inner class.
5- We can declare static methods with same signature in subclass, but it is not considered overriding as there won’t be any run-time polymorphism. Hence the answer is ‘No’.
6- We can call parent class method in overridden method using super keyword.
// A Simple Java program to demonstrate overriding and super keyword in Java Base Class
class Parent{
void show() {
System.out.println( "Parent's class method show()" );
}
}
// Inherited class
class Child extends Parent{
// This method overrides show() of Parent
void show() {
super .show();
System.out.println( "Child's class method show()" );
}
}
// Driver class
class Main{
public static void main(String[] args) {
Parent obj = new Child();
obj.show();
}
}
|
Output:
Parent's class method show()
Child's class method show()
More question related to overriding
- Can we override private methods in Java?
- Why Overriding toString() method in Java?
post a comment