Tutorial

Overriding vs Overloading in Java

Updated on October 1, 2022
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.

Overriding versus overloading in Java
Screenshot of Java code with arrows pointing at instances where overloading and overriding are occurring.

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 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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author


Default avatar

Sr Technical Writer

Sr. Technical Writer@ DigitalOcean | Medium Top Writers(AI & ChatGPT) | 2M+ monthly views & 34K Subscribers | Ex Cloud Consultant @ AMEX | Ex SRE(DevOps) @ NUTANIX


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
February 3, 2022

Thanks you. because l have irritating this concept understanding after that your implement concept and explain easy understanding ,so iam very happy

- ambika

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    January 31, 2022

    Its good but with an Example its better for more Understanding. Overall efforts are good.

    - Shriganesh Mane

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      September 10, 2021

      Great explanation of the topic. You made it simple and easy to understanding. Thank you.

      - David

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        June 9, 2021

        Nice, concise explanations. Referring my students here for this topic.

        - Mark Miller

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          May 21, 2021

          thanks for clear explanation of difference

          - MOHAMMAD AHTISHAM

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            August 24, 2019

            hello, very happy with this website.

            - ranjit vamadevan

              Try DigitalOcean for free

              Click below to sign up and get $200 of credit to try our products over 60 days!

              Sign up

              Join the Tech Talk
              Success! Thank you! Please check your email for further details.

              Please complete your information!

              Become a contributor for community

              Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

              DigitalOcean Documentation

              Full documentation for every DigitalOcean product.

              Resources for startups and SMBs

              The Wave has everything you need to know about building a business, from raising funding to marketing your product.

              Get our newsletter

              Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

              New accounts only. By submitting your email you agree to our Privacy Policy

              The developer cloud

              Scale up as you grow — whether you're running one virtual machine or ten thousand.

              Get started for free

              Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

              *This promotional offer applies to new accounts only.