QuizCure
Find Available Solution Here!

Difference Between Method Overloading and Method Overriding in Java

Deepak Dec 08, 2024

Overriding is a feature in an object-oriented programming language that allows redefining the parent class method in the child class (Subclass).

The Following conditions must met for overriding method:

  • Method name: Child Class method must have the same method name as in parent class method.
  • Method argument: Child Class method must have the same method argument as in parent class method.
  • Return Type: Child Class method must have the 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 Steps 2 & 3 Object Reference is the Paranet class Vehicle which Refers to the child class Car & Truck. During runtime, it looks for the Child class method move and executes. This feature is known as POLYMORPHISM.

In this post we are going to learn the Difference Between Method Overloading and Method Overriding in Java and related concepts with examples so please continue reading.

What is the difference between overriding methods and overloading methods?


Overloading Overriding
Scope Defined within a class Occurs in relationship classes. Child class overrides 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 have the same argument type and number of arguments but different return type
The Overridden method should have the same return type as in the parent class method.
Before the Java 5 version, it was not possible to keep different return types but since Java 5 onwards it has supported 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 the parent static method with the 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.

Was this post helpful?

Send Feedback

Practice Multiple-Choice Questions

  • ?
    A static method in Java can be overridden. Is it correct or incorrect?
    Options are:
  • ?
    A final method in Java can be overridden. Is it correct or incorrect?
    Options are:
    Try your hand at more Multiple-choice Exercises

Connect With QuizCure


Follow Us and Stay tuned with upcoming blog posts and updates.

Contributed By

Deepak

Deepak

QC STAFF
51 Posts
  • PHP
  • JAVA
  • PYTHON
  • MYSQL
  • SEO

You May Like to Read

Scroll up