Overriding-:Whatever methods parent has by default available to the child through inheritance if child class not satisfy with parent class implementation then child is allowed to redefine based on its requirement this process is called overriding.
The parent class method which is overridden is called overridden method and child class method which is overriding is called overriding method.
Example-:
class Vehicle{
//Overridden method
public void Bus(){
System.out.print("Bus is fast");
}
class Bike extends Vehicle{
public void Bus(){
System.out.print("Bus is fast");
}
class Bike extends Vehicle{
//Override method
public void Bus(){
System.out.print("Bus is slow");
}
public static void main(String[] args){
Bike b=new Bike();
b.Bus();
}
}
public void Bus(){
System.out.print("Bus is slow");
}
public static void main(String[] args){
Bike b=new Bike();
b.Bus();
}
}
Case 1-:In overriding method resolution always takes care by JVM based on run time object and hence overriding is also considered as run time polymorphism or dynamic polymorphism or late binding.
class Vehicle{
//Overridden method
public void Bus(){
System.out.print("Bus is fast");
}
class Bike extends Vehicle{
public void Bus(){
System.out.print("Bus is fast");
}
class Bike extends Vehicle{
//Override method
public void Bus(){
System.out.print("Bus is slow");
}
}
public void Bus(){
System.out.print("Bus is slow");
}
}
class Test{
public static void main(String[] args){
Vehicle v=new Vehicle();
v.Bus(); //Bus is Fast
b.Bus(); //Bus is slow
Vehicle v1=new Bike();
v1.Bus(); //Bus is slow
}
}
Rules for overriding-:
- In overriding method name and arguments types must be matched that is methods signatures must be same.
- In overriding return types must be same, but this rule is applicable until 1.4v only for 1.5v onwards we can take covariant return types according to this child class method. Return type need not be the same as the parent method return type, its child type also allowed.
Example-:
class P{
public Object m(){
return null;
}
}
class C extends P{
public String m(){
return "null";
}
compile time error in 1.4v
- Covariant return type concept applicable only for Objects types but not for primitive types.
- Parent class private method not available to the child and hence overriding concept not applicable for private methods.
- Based on our requirement we can define exactly same private method in child class, it is valid but not overriding.
Example-:
class P{
private void m(){
System.out.print("Hello");
}
}
class C extends P{
private void m(){
System.out.print("Welcome");
}
- We can't override parent class final methods in child classes if we are trying to override we will get compile time error.
Example-:
class P{
public final void m(){
System.out.print("Hello");
}
}
class C extends P{
public void m(){
System.out.print("Welcome");
}
}
compile time error-:m() in C cannot override m() in P overridden method is final
- Parent class abstract method we should override in child class to provide implementation.
Example-:
abstract class P{
public abstract void m();
}
class C extends P{
public void m(){
System.out.print("Welcome");
}
}
- We can override non-abstract method as abstract method. The main advantage of this approach is we can stop the availability of parent method implementation in the next level child classes.
Example-:
class P{
public void m(){
}
}
abstract class C extends P{
public abstract void m();
}
In overriding, the following modifiers won't keep any restriction.
- synchronized
- native
- strictfp
- If child class method throws any checked exception compulsory parent class method should throw the same checked exception or its parent otherwise we will get compile time error, but there are no restriction for unchecked exceptions.
#valid
P: public void m() throws Exception
C: public void m()
#invalid
P: public void m()
C: public void m() throws Exception
#valid
P: public void m() throws Exception
C: public void m() throws IOException
#invalid
P: public void m() throws IOException
C: public void m() throws Exception
#valid
P: public void m() throws IOException
C: public void m() throws FileNotFoundException,EOFException
#invalid
P: public void m() throws IOException
C: public void m() throws EOFException, InterruptedException
#valid
P: public void m() throws IOException
C: public void m() throws AE,NPE,CCE
Example-:
class P{
public void m() throws IOException
{
}
}
class C extends P{
public abstract void m() throws EOFException,InterruptedException
{
}
}
- Overriding with respect to static methods.
- We can't override static method as non-static otherwise we will get compile time error.
Example-:
class P{
public static void m(){
}
}
class C extends P{
public void m(){
}
}
compile time error-: m() in c can't override m() in P ; overridden method is static
- Similarly we can't override a non-static method as static.
Example 2-:
class P{
public void m(){
}
}
class C extends P{
public static void m(){
}
}
compile time error-: m() in c can't override m() in P ; overriding method is static.
- If both parent and child method are static then we won't get any compile time error it seems overriding concept applicable for static methods, but it is not overriding and it is method hiding.
Example-:
class P{
public static void m(){
}
}
class C extends P{
public static void m(){
}
}
Method Hiding
all rules of method hiding are exactly the same as overriding, accept the following differences.
Example-:
class P{
public static void m(){
System.out.print("Parent");
}
}
class C extends P{
public static void m(){
System.out.print("Child");
}
}
class Test{
public static void main(String... args){
P p=new P();
p.m(); // parent
C c=new C();
c.m(); // child
P p1=new C();
p1.m(); // parent
}
}
If both parent and child class methods are non-static, then it will become overriding output is.
Parent
child
child