Purpose of method overriding in Java
We perform overriding when we need to perform some specific customized set of operations with the available input sets of data after inheriting it from the base class.
The tradition, culture, and rituals we inherit from our ancestors and customized by us to the present scenario and needs is a live example of method overriding!
Method Overriding refers to overriding the generic (default) behavior and functionality of method in the child class as per its own tailor-made custom requirements. It refers to take a general approach to specialization.
Whatever the Parent has by default available to the Child through inheritance, if the Child is not satisfied with Parent class method implementation then Child is allowed to redefine that Parent class method in Child class in its own way this process is called overriding.
The Parent class method which is overridden is called overridden method. The Child class method which is overriding is called overriding method.
Example
class Parent {
public void property(){
System.out.println("cash+land+gold+house");
}
public void marry() {
System.out.println("Anamika"); //overridden method
}
}
class Child extends Parent{ //overriding
public void marry() {
System.out.println("Anna/Katrina/Rashmika/Anushka/Nayana"); //overriding method
}
}
class OverridingTest {
public static void main(String[] args) {
Parent p=new Parent();
p.marry();//Anamika(parent method)
Child c=new Child();
c.marry();//Anna/Katrina/Rashmika/Anushka/Nayana(child method)
Parent p1=new Child();
p1.marry();//Anna/Katrina/Rashmika/Anushka/Nayana(child method)
}
}
In overriding method resolution is always takes care by the JVM based on runtime object hence overriding is also considered as runtime polymorphism or dynamic polymorphism or late binding.
The process of overriding method resolution is also known as dynamic method dispatch. Note: In overriding runtime, the object will play the role and reference type is dummy
Rules for overriding
1). In overriding method names and arguments must be the same. That is the method signature must be the same. Until 1.4 version the return types must be the same but from 1.5 version onwards co-variant return types are allowed. According to this Child class method return type need not be the same as Parent class method return type its Child types also allowed.
Example:
class Parent {
public Object methodsayHello() {
return null;
}
}
class Child extends Parent {
public String methodsayHello() {
return null;
}
}
C:> javac -source 1.4 Parent.java //error
It is valid in "1.5" but invalid in "1.4".
2). Private methods are not visible in the Child classes hence overriding concept is not applicable for private methods. Based on our own requirement we can declare the same Parent class private method in child class also. It is valid but not overriding.
Example:
class Parent
{
private void methodsayHello()
{
}
class Child extends Parent
{
private void methodsayHello()
{
// Its valid but its method hiding
}
}
3). Parent class final methods we can't override in the Child class.
class Parent {
public final void methodsayHello() {}
}
class Child extends Parent{
public void methodsayHello(){}
// Compile time error:
methodsayHello() in Child cannot override methodsayHello()
in Parent; overridden method is final
}
4). Parent class non-final methods we can override as final in child class. We can override native methods in the child classes.
5). We should override Parent class abstract methods in Child classes to provide the implementation.
Example:
abstract class Parent {
public abstract void methodOne();
}
class Child extends Parent {
public void methodOne() { }
}
6). We can override a non-abstract method as abstract , it is useful to stop the availability of Parent method implementation to the next level child classes.
class Parent {
public void methodsayHello() { }
}
abstract class Child extends Parent {
public abstract void methodsayHello();
}
7). Synchronized, strictfp, modifiers won't keep any restrictions on overriding.
8). While overriding we can't reduce the scope of access modifier.
Example:
class Parent {
public void methodOne() { }
}
class Child extends Parent {
protected void methodOne( ) { }
}
Output:
Compile time error :
methodOne() in Child cannot override methodOne() in Parent;
attempting to assign weaker access privileges; was public
In this article, we have seen a method overriding in Java.
0 Comments
Post a Comment