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 allow to redefine that Parent class method in Child class in its own way this process is called overriding.
Method overriding refers to when a piece of code contains two or more methods with the same name, but each one performs a different task. Taking the literal sense of the name, this means that one approach must take precedence over another. This idea refers to redefining a base class method with the same method signature in a derived class.
In overriding method resolution is always takes care by JVM based on runtime object hence overriding is also considered as runtime polymorphism or dynamic polymorphism or late binding.
class Parent { public void method1() { System.out.println("Parent Method 1"); } public void method2() { System.out.println("Parent Method 2"); } } class Child extends Parent { public void method1() { System.out.println("Child Method 1"); } } class Main { public static void main(String[] args) { Parent p=new Parent(); p.method1();//parent method 1 Child c=new Child(); c.method1();//child method 1 Parent p1=new Child(); p1.method1();//child method 1 } }
In overriding method names and arguments must be same. That is method signature must be same.
In Java, Until 1.4 version the return types must be same but from 1.5 version onwards co-variant return types are allowed.
According to this Child class method return type need not be same as Parent class method return type its Child types also allowed.
Parent class non final methods we can override as final in child class. We can override native methods in the child classes.
We should override Parent class abstract methods in Child classes to provide implementation
We can override Parent class non abstract method as abstract to stop availability of Parent class method implementation to the Child classes.
While overriding we can’t reduce the scope of access modifier.
Synchronized, strictfp, modifiers won’t keep any restrictions on overriding.
Consider the case where Bank is a class that provides functionality for calculating interest rates. The interest rate, on the other hand, differs between banks. SBI, ICICI, and AXIS banks, for example, might provide interest rates of 8%, 7%, and 9%, respectively.
class Bank{ int getRateOfInterest(){ return 0; } } class SBI extends Bank{ int getRateOfInterest(){ return 8; } } class ICICI extends Bank{ int getRateOfInterest(){ return 7; } } class AXIS extends Bank{ int getRateOfInterest(){ return 9; } } class Main{ public static void main(String args[]){ SBI s=new SBI(); ICICI i=new ICICI(); AXIS a=new AXIS(); System.out.println("SBI Rate of Interest: "+s.getRateOfInterest()); System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest()); System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest()); } }
Method Overriding is used to provide specific implementation of a method that is already provided by its super class.
Helps in writing generic code based on parent class or interface as object resolution happens at runtime
Defines what behavior a class can have and implementation of behavior has been taken care by class which is going to implement.
Provides multiple implementation of same method and can invoke parent class overridden method using super keyword
Method Overriding is used for Runtime Polymorphism