Method Overloading And Overriding In Java

Introduction

Method overloading and overriding are two concepts that make Java programming enjoyable. They help developers save time, code, and effort when they have to write code repeatedly.

This post is about method overloading and overriding in java. These two programming concepts getting mainly used for extending existing methods or classes. This article teaches us more about its basic concepts and the difference between method overloading and method overriding.

What is Method Overloading in Java?

The method overloading technique allows a method to share the same name but differ based on its arguments. It is a compile-time polymorphism.

We can't overload return types. Overloading static methods is allowed, but the input parameters must be different. We cannot overload two methods if they only differ by a static keyword. It is similarly possible to overload the main() method, just as you can with other static methods.

To understand how Java method overloading works, let's take a look at a simple program.

public class Div{
public int div(int x , int y){
        return (x / y); }

public int div(int x , int y , int z){
        return ((x - y ) / z); }

public static void main(String args[]){
Div ob = new Div();
System.out.println(ob.div(20 , 2));
System.out.println(ob.div(20, 2 , 3));
}
}


Output:
10
6

write your code here: Coding Playground

What is Method Overriding in Java?

Overriding methods is a runtime polymorphism. In method overriding, a derived class provides specific implementations of methods present in the base class.

If both classes contain methods with the same name, arguments, or parameters, then one will always override the other. It depends on the object which method will get executed.

In any object-oriented programming language, overriding requires a subclass or child class to provide variants of methods previously provided by a superclass or parent class.

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


class Car extends Vehicle
  //declearing similar method as in the parent class 
  void run(){
      System.out.println("Car is running smoothly");
  } 
}

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



Output

Car is running smoothly

write your code here: Coding Playground

Difference Between Method Overloading And Method Overriding In Java

The difference between overloading and overriding a method in Java is quite complex. Following is a list of differences between overloading and overriding methods:

Method Overloading

Method Overriding

Programmers use method overloading to make their code more readable.

A method override provides the specific implementation of a method in its superclass.

The method overloading occurs within classes.

Method overriding occurs in two classes.

Compile time polymorphism is exhibited by method overloading.

Polymorphism at run time is demonstrated by method overriding.

Parameters must be different when overloading a method.

Parameters must be the same when overriding a method.

Conclusion

In this article, we discussed the difference between method overloading and method overriding. Java allows method overloading and method overriding in classes, which is called polymorphism. Polymorphism is the ability to solve a problem with more than one form or method.

Overriding methods allow you to change the behavior of a specific method in the hierarchy tree. This blog explains the meaning and difference between overloading and overriding so that it becomes clear when to use it