Method Overriding

  • June, 2021
  • 0 Views
  • 1
  • 2

Overriding is a feature in object-oriented programming language that allows to redefine the parent class method in child class (Sub class).

Following conditions must met for overriding method:

  • Method name : Child Class method must have same method name as in parent class method.
  • Method argument : Child Class method must have same method argument as in parent class method.
  • Return Type: Child Class method must have same return type as in parent class method.
  • Relationship : There must be a relationship (inheritance) defined between parent and child class.
  // Base (Parent) Class
class Vehicle {
    
	void move()
	{
		System.out.println("Vehicle Move..");
	}
}

// Inherited class
class Car extends Vehicle {

// This method overrides move() of Parent Class Vehicle
	@Override
	void move()
	{
		System.out.println("Car move");
	}
}

// Inherited class
class Truck extends Vehicle {
	// This method overrides move() of Parent Class Vehicle
	@Override
	void move()
	{
		System.out.println("Truck move");
	}
}

public class OverRiding {

 
    public static void main(String[] args) {

     
     // Step 1   
     Vehicle vehicle = new Vehicle();
     vehicle.move();
     
     //OutPut: Vehicle Move..
     
  
      // Step 2   
     Vehicle vehicleCar = new Car();
     vehicleCar.move();
     //OutPut:  Car move
  
     
     
     // Step 3   
     Vehicle vehicleTruck = new Truck();
     vehicleTruck.move();
     //Output: Truck move
    }

}

In Step 2 & 3 Object Reference is Paranet class Vehicle which Refer to child class Car & Truck. During runtime it looks for Child class method move and execute. This feature is known as POLYMORPHISM.

1. What is the difference between overriding methods and overloading methods?

Overloading Overriding
Scope Defined within a class Occurs in relationship classes. Child class override parent class method.
Method Name Method name must be the same to achieve method Overloading. Method name must be the same between parent and child class to achieve method Overriding.
Return type Return type must be the same for methods having the same number of arguments with the same type. Following are Wrong and raise compile time error :
Int abc(int a, string b) {
}

String abc(int a, string b) {
}
Why? Because both methods abc having same argument type and number of argument but different return type
Overridden method should have the same return type as in the parent class method.
Before java 5 version it was not possible to keep different return type but since Java 5 onwards it Supports covariant return types.
Argument list Should be different for each overloading method if the same type is used.
 Correct:
Int abc(int a, int b) {
}

int abc(int a, int b, int c) {
}

Wrong: 

Int abc(int a, int b) {
}

int abc(int a, int b) {
}

Will raise compile time error.

                        
Should be the same and same type in parent and child class,.
Polymorphism Implements compile time polymorphism. Overloading gives better performance due to compile time polymorphism over overriding. Implements runtime polymorphism.
Static methods Overloading is possible for Static methods. Overriding is not possible for static methods. We can not override parent static method with same name and arguments.
Private methods Private methods can be overloaded Private methods can not be overridden.
Final method Final method can be overloaded Final method can not be overridden.

No comments posted yet. Be the first to post comments. Thanks

Write Your Comments