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-:
public void Bus(){
System.out.print("Bus is fast");
}
class Bike extends Vehicle{
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.
public void Bus(){
System.out.print("Bus is fast");
}
class Bike extends Vehicle{
public void Bus(){
System.out.print("Bus is slow");
}
}
b.Bus(); //Bus is slow
}
- 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.
- 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.
- We can't override parent class final methods in child classes if we are trying to override we will get compile time error.
- Parent class abstract method we should override in child class to provide implementation.
- 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.
- 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
- Overriding with respect to static methods.
- We can't override static method as non-static otherwise we will get compile time error.
- Similarly we can't override a non-static method as 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.

