Method Overriding in Java

In the session , we talked about super classes and sub classes. If a class inherits a method from its super class, then there is a chance to override the method provided that it is not marked final. The benefit of overriding is: ability to define a behavior that's specific to the subclass type which means a subclass can implement a parent class method based on its requirement. In object-oriented terms, overriding means to override the functionality of an existing method.

Rules for Method Overriding

  • The argument list should be exactly the same as that of the overridden method.
  • The return type should be the same or a sub type of the return type declared in the original overridden method in the superclass.
  • The access level cannot be more restrictive than the overridden method's access level. For example: if the superclass method is declared public then the overridding method in the sub class cannot be either private or protected.
  • Instance methods can be overridden only if they are inherited by the subclass.
  • A method declared final cannot be overridden.
  • A method declared static cannot be overridden but can be re-declared.
  • If a method cannot be inherited, then it cannot be overridden.
  • A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final.
  • A subclass in a different package can only override the non-final methods declared public or protected.
  • Constructors cannot be overridden

Advantages Java Method Overriding

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

Understanding problem without method overriding

 class Vehicle {

	void run() {
		System.out.println("Vehicle is running");
	}

}

class Bike extends Vehicle {
	public static void main(String args[]) {
		Bike obj = new Bike();

		obj.run();

	}

}

Output:Vehicle is running

Problem is that I have to provide a specific implementation of run() method in subclass that is why we use method overriding.

Note

In a subclass, you can overload the methods inherited from the superclass. Such overloaded methods neither hide nor override the superclass methods—they are new methods, unique to the subclass

Example of method overriding

we have defined the run method in the subclass as defined in the parent class but it has some specific implementation. The name and parameter of the method is same and there is IS-A relationship between the classes, so there is method overriding.

 
 class Car {

	void run() {

		System.out.println("Car is running");
	}

}

class Bike extends Car {
	void run() {

		System.out.println("Bike is running Fast");
	}

	public static void main(String args[]) {
		Bike obj = new Bike();
		obj.run();
	}
}

 
 Output:Bike is running Fast
 

Difference between method Overloading and Method Overriding in java

There are three basic differences between the method overloading and method overriding. They are as follows:

Method Overloading Method Overriding
1) Method overloading is used to increase the readability of the program. Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
2) method overloading is performed within a class. Method overriding occurs in two classes that have IS-A relationship.
3) In case of method overloading parameter must be different. In case of method overriding parameter must be same.

Can we override static method?

Ans :No, static method cannot be overridden. It can be proved by runtime polymorphism, so we will learn it later.

Why we cannot override static method?

Ans : because static method is bound with class whereas instance method is bound with object. Static belongs to class area and instance belongs to heap area.

Can we override java main method?

Ans :No, because main is a static method.