Difference between Overloading and Overriding in Java

Key difference: Overloading means having two methods of the same class with the same name and with different parameter types, whereas overriding means having two different methods in same class with the same parameter types, but with different implementations. 

The methods overriding and overloading are two concepts or techniques used in java programming languages. Both the concepts allow the programmer to provide different implementations for methods under the same name. This article differentiates between the two concepts of programming.

Function overloading or method overloading allows creating several methods under the same name, in the same class but different from each other in the input and the output type of the function. It is simply defined as the ability of one function to perform different tasks.

In overloading, the method implementations share the same name because they perform similar tasks. Also, overloading is considered polymorphic in nature. The functions having different implementations are dependent on their specified argument types. It is usually associated with static programming languages which enforces type checking in function calls. It is also considered practical as it allows the programmer to write a number of different methods in the same class. However, in overloading the runtime processor changes the name of all the overloaded methods, which can be a problem.

Method overriding, in object oriented programming, is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. The implementation in the subclass overrides or replaces the implementation in the parent-class by providing a method under the same name, same parameters or signature, and same return type as the method in the parent class.

In method overriding, the functions are coded to carry out specific tasks in a program. Overriding depends upon the presence of a base class function for its appearance. Here, the executed function is determined by the object that is used to invoke it i.e. if an object of a parent class is used to invoke the method, then the function in the parent class will be executed. Overriding is polymorphic in nature; it helps to design programs based on the first implicit parameter, which can be resolved at runtime. There are some languages in java allow a programmer to prevent a method from being overridden.

Comparison between Overloading and Overriding:

 

Overloading

Overriding

Definition

It means having methods of the same class under the same name, but each method has different parameters or has same parameters with different types and order.

It means having a sub class with same methods under same name and exactly the same type of parameters and the same return type as a super class.

Meaning

It means that more than one method share the same name in the same class but have a different signature.

It means that the method of base class is re-defined in the derived class with the same signature.

Behavior

It adds or extends to the methods behavior.

It changes the existing behavior of a method.

Polymorphism

It is a compile time polymorphism.

It is a run time polymorphism.

Static method

The static method can be overloaded.

The static method cannot be overridden.

Bond

Static bond.

Dynamic bond.

Inheritance

It may or may not require inheritance.

It always requires inheritance.

Association

It is usually associated with static program languages.

It is usually associated with object orientated programs.

Signature

Methods have different signature.

Methods must have same signature.

Speed

It is faster than overriding.

It is slow as compared to overloading.

Classes

It does not require more than one class for overloading.

It requires at-least two classes for overloading.

Level

Methods can have any access level.

Methods have same or wide access level.

Example

Class Add

{

   int sum(int a, int b)

   {

     return a + b;

   }

  int sum(int a)

  {

    return a + 10;

   }

}

Class A  // Super Class

{

  void display(int num)

  {

     print num ;

   }

}

//Class B inherits Class A

Class B //Sub Class

{

  void display(int num)

  {

     print num ;

   }

}

Image Courtesy: java2s.com, javatutorialhub.com

Most Searched in Education and References Most Searched in Society and Culture
Most Searched in Sports Most Searched in Home and Garden
AGP vs PCI
Plasma Membrane vs Cell Wall
Sunmica vs Laminate
Revenue vs Receipt

Add new comment

Plain text

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.