Method Overriding


Method overriding is when a subclass (child class) has the same method as declared in its parent/super class.

It is used for runtime polymorphism to provide specific implementation of a method that is already provided by its super class.

For method overriding, the method must have same name as well as same parameters as in its parent class.


Example:

class Bank
{  
    int getRateOfInterest()
    {
    return 0;
    }
}

class SBI extends Bank
{  
    int getRateOfInterest()
    {
        return 6;
    }
}

class PNB extends Bank
{  
    int getRateOfInterest()
    {
        return 5;
    }  
}  

class MethodOverriding
{  
    public static void main(String args[])
    {  
        SBI s=new SBI();  
        PNB p=new PNB();    
        System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
        System.out.println("PNB Rate of Interest: "+p.getRateOfInterest());
    }
}

Output:

6
5