Overloading and Overriding Methods in Java

Two terms commonly associated with methods in Java are overloading and overriding. These two concepts will be discussed in the following sections.

Method Overloading

Method overloading is the process of defining more than one method having the same name in the same class (or in the same inheritance tree).

Rules to define overloaded methods:

  • The methods must have the same name.
  • The methods must have different argument lists.
  • They may have same or different return types.
  • Their access levels may be same or different.

The correct method to be executed by the system is decided by the Java compiler at compile time, and this is called static polymorphism. The following example demonstrates method overloading:

public class Test {

       public static int add(int a, int b){    //adds 2 numbers

        return a + b;

    }

       public static String add(String a, String b){  //concatenates 2 Strings with a space in between

        return a + ” ” + b;

    }

       public static void main(String[] args){

           System.out.println(“4 + 5 = ” + add(4, 5));

        //calls 1st method

           System.out.println(“Method + Overloading = ” + add(“Method”, “Overloading”));

        //calls 2nd method

    }

   }

The output of the above program is:

4 + 5 = 9

Method + Overloading = Method Overloading

Method Overriding

Method overriding means giving a new definition to an existing method in a class, in one of its subclasses. This is done to redefine the behaviour of objects of the subclass.

Rules to override a method:

  • The overriding method should be present in the subclass of the class in which the overridden method is present.
  • The overriding and overridden methods should have the same name and argument list.
  • The two methods should have the same return type. Or the return type of the overriding method should be a subclass of that of the overridden method.
  • The access modifier of the overriding method must be either the same as or less restrictive than that of the overridden method.

The method to be executed is decided at runtime (not at compile time), and this is called dynamic polymorphism. The following example demonstrates method overriding:

class A {

    public void display(){

        System.out.println(“Executing from class A”);

    }

}

class B extends A {

    public void display(){               //override the method display()

        System.out.println(“Executing from class B”);

   

    }

}

public class Test1 {

    public static void main(String[] args) {

        A objA = new A();

        A objB = new B();              //an A reference, but a B object

        objA.display();

        objB.display();

   

    }

 

}

The above code prints:

Executing from class A

Executing from class B

We have seen the core concepts of ‘Overloading and Overriding’ in Java in this post… Join me as I uncover more Java concepts in subsequent posts…

Bala Manikandan is a 12th grade student in India. He is an Oracle certified Associate(OCAJP 8) Bright and Intelligent, Computers and Math are his favorite subjects!
He hopes to do path breaking research in the Computer Science field!

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.