URL: https://www.progressiverobot.com/overriding-vs-overloading-in-java/

Introduction

_Overriding_ and _overloading_ are the core concepts in Java programming. They are the ways to implement polymorphism in our Java programs. Polymorphism is one of the OOPS Concepts.

When the method signature (name and parameters) are the same in the superclass and the child class, it's called _overriding_. When two or more methods in the same class have the same name but different parameters, it's called _overloading_.

Comparing overriding and overloading

overriding illustration for: Comparing overriding and overloading
Overriding Overloading
Implements "runtime polymorphism" Implements "compile time polymorphism"
The method call is determined at runtime based on the object type The method call is determined at compile time
Occurs between superclass and subclass Occurs between the methods in the same class
Have the same signature (name and method arguments) Have the same name, but the parameters are different
On error, the effect will be visible at runtime On error, it can be caught at compile time

Overriding and overloading example

Here is an example of overloading and overriding in a Java program:

				
					package com.journaldev.examples;

import java.util.Arrays;

public class Processor {

 public void process(int i, int j) {
  System.out.printf("Processing two integers:%d, %d", i, j);
 }

 public void process(int[] ints) {
  System.out.println("Adding integer array:" + Arrays.toString(ints));
 }

 public void process(Object[] objs) {
  System.out.println("Adding integer array:" + Arrays.toString(objs));
 }
}

class MathProcessor extends Processor {

 @Override
 public void process(int i, int j) {
  System.out.println("Sum of integers is " + (i + j));
 }

 @Override
 public void process(int[] ints) {
  int sum = 0;
  for (int i : ints) {
   sum += i;
  }
  System.out.println("Sum of integer array elements is " + sum);
 }

}
				
			

Overriding

The process() method and int i, int j parameters in Processor are overridden in the child class MathProcessor. Line 7 and line 23:

				
					public class Processor {

    public void <^>process(int i, int j)<^> { /* ... */ }

}

/* ... */

class MathProcessor extends Processor {
 
    @Override
    public void <^>process(int i, int j)<^> {  /* ... */ }

}
				
			

And process() method and int[] ints in Processor are also overridden in the child class. Line 11 and line 28:

				
					public class Processor {

    public void <^>process(int[] ints)<^> { /* ... */ }

}

/* ... */

class MathProcessor extends Processor {

    @Override
    public void <^>process(Object[] objs)<^> { /* ... */ }

}
				
			

Overloading

The process() method is overloaded in the Processor class. Lines 7, 11, and 15:

				
					public class Processor {

    public void <^>process(<^>int i, int j<^>)<^> { /* ... */ }

    public void <^>process(<^>int[] ints<^>)<^> { /* ... */ }

    public void <^>process(<^>Object[] objs<^>)<^> { /* ... */ }

}
				
			

Conclusion

In this article, we covered overriding and overloading in Java. Overriding occurs when the method signature is the same in the superclass and the child class. Overloading occurs when two or more methods in the same class have the same name but different parameters.