Compare the Difference Between Similar Terms

Difference Between Overloading and Overriding in Java

Key Difference – Overloading vs Overriding in Java
 

Object-Oriented Programming (OOP) is a major paradigm in software development. It is a methodology to design a program using classes and objects. A class is a blueprint. It describes what should contain in the object. It defines the properties or attributes and the methods that the object should consist of. Therefore, an object is an instance of a class. These objects communicate with other objects. One major concept of OOP is Polymorphism. It is the ability for an object to behave in multiple ways. Polymorphism is categorized into two sections which are overloading and overriding. This article discusses the difference between these two in Java. The key difference between overloading and overriding in Java is that the Overloading is the ability to create multiple methods of the same name with different implementations and Overriding is to provide an implementation for a subclass method that already exists in the superclass.

CONTENTS

1. Overview and Key Difference
2. What is Overloading in Java
3. What is Overriding in Java
4. Similarities Between Overloading and Overriding in Java
5. Side by Side Comparison – Overloading vs Overriding in Java in Tabular Form
6. Summary

What is Overloading in Java?

Overloading is the ability to create multiple methods of the same name with different implementations. Refer the below Java code.

Figure 01: Java program that explains Overloading with different number of arguments

According to the above program, class A contains two methods with the same name. The first sum method has two parameters. The second sum method has three parameters. When creating an object of type A and calling sum(2,3) , it will call sum method with two parameters that are sum(int a, int b) and returns 5. When creating an object of type A and calling sum(2,3,4), it will call the other sum method with three parameters which is the sum(int a, int b, int c) and returns 9.

The method name is the same but number of parameters are different. It can be observed that the same object is behaving differently. This concept is known as overloading. It is also referred as Static Binding or Compiles Time Polymorphism.

There can also be overloaded with different data types. Refer the below Java code.

Figure 02: Java program which explains Overloading with different number of arguments

According to the above program, class A consist of two methods with the same name. The sum(int a, int b) method receives two integer values. The sum(double a double b) receives two double values. When creating the object of type A and calling sum(2,3), it will call sum(int a, int b) and return the value 5. When calling sum(3.4, 5.6), it will call sum(double a double b) and return the value 9.0. In this example, the methods have the same name, but a different type of variables. This is also overloading.

What is Overriding in Java?

In Java, it is possible to build subclasses with already existing classes. Rather than creating the new class from the beginning, it is possible to use the properties and methods of the already existing class. The existing class is the superclass, and the derived class is the subclass. When the subclass provides an implementation for a method, which is already in the superclass, it is known as overriding. Refer the below Java program.

Figure 03: Java program for overriding

According to the above program, Class A is having a method display(). Class B is extending from class A, so the properties and methods of class A are accessible by class B. Class B has method display() with a specific implementation. When creating an object of type, A and calling the display method, it will give the output B. Even though class A has a display method, it is overridden be class B display method. Subclass is implementing a method already exist in the superclass.

This concept is a type of polymorphism and known as overriding. It is also called as Late Binding, Dynamic Binding, Runtime Polymorphism.

What are the Similarities Between Overloading and Overriding in Java?

What is the Difference Between Overloading and Overriding in Java?

Overloading vs Overriding in Java

Overloading in Java is the ability to create multiple methods of the same name with different implementations. Overriding in Java is providing a specific implementation in subclass method for a method already exist in the superclass.
 Parameters
In overloading, the methods have the same name but a different number of parameters or a different type of parameters. In overriding, the methods have the same name and parameters must be the same.
Themes
Overloading occurs within the class. Overriding occurs within the two classes that have an inheritance relationship.
 Synonyms
Overloading is called compiled time polymorphism. Overriding is called run time polymorphism.

Summary – Overloading vs Overriding in Java

Polymorphism is a major concept in Object Oriented Programming. It provides the ability for an object to behave in multiple ways. This can be of either overloading or overriding. Overloading is the compile-time polymorphism, and overriding is the runtime polymorphism. They are useful in software application development. The difference between overriding and overloading is that Overloading is the ability to create multiple methods of the same name with different implementations and Overriding is providing a specific implementation in subclass method for a method already exist in the superclass. It is possible to implement both overloading and overriding in Java.

Download the PDF Overloading vs Overriding in Java

You can download PDF version of this article and use it for offline purposes as per citation note. Please download PDF version here Difference Between Overloading and Overriding in Java

Reference:

1.tutorialspoint.com. “Java Overriding.” The Point. Available here 
2.“Method Overloading in Java – Javatpoint.” Available here
3.“Method Overriding in Java – javatpoint.” Available here