Method overloading-overriding (Java)

Send Us a Sign! (Contact Us!)
Word PDF XPS XML

The difference between overriding and overloading in Java is a common source of confusion – but it is fairly easy to understand with the examples we present below. Let’s start the discussion by talking more about method overloading first. Method overloading in Java occurs when two or more methods in the same class have the exact same name but different parameters (remember that method parameters accept values passed into the method).

Now, two or more methods with the same name in the same class sounds simple enough to understand. But, what do we mean exactly by different parameters? Well, let’s consider a very simple example.

Method overloading-overriding (Java)

Suppose we have a class called TestClass which has two methods, and both methods have the same name. Let’s say that name is “someMethod”. Those two methods would be considered to be “overloaded” if if one or both of these conditions is true:

The conditions for method overloading:

  1. The number of parameters is different for the methods.
  2. The parameter types are different (like changing a parameter that was a float to an int).

How to NOT overload methods

It’s also very important to understand that method overloading is NOT something that can be accomplished with either, or both, of these two things:

Method overloading-overriding (Java)

  1. Just changing the return type of the method. If the return type of the method is the only thing changed, then this will result in a compiler error.
  2. Changing just the name of the method parameters, but not changing the parameter types. If the name of the method parameter is the only thing changed then this will also result in a compiler error.

Well, here are some very helpful examples of where overloading would be both valid and invalid – pay attention to the comments as well:

Examples of Method Overloading in Java (valid and invalid)

//compiler error - can't overload based on the   
//type returned -
//(one method returns int, the other returns a float):    

int changeDate(int Year) ; float changeDate (int Year);
//compiler error - can't overload by changing just //the name of the parameter (from Year to Month):
int changeDate(int Year); int changeDate(int Month) ;
//valid case of overloading, since the methods //have different number of parameters:
int changeDate(int Year, int Month) ; int changeDate(int Year);
//also a valid case of overloading, since the //parameters are of different types:
int changeDate(float Year) ; int changeDate(int Year);

Another important point to remember is that overloading is a compile time phenomenon. This just means that the compiler determines whether a given method(s) is correctly overloaded, and if not a compiler error is returned as shown in the examples above.

Another important point to remember is that overloading is a compile time phenomenon. This just means that the compiler determines whether a given method(s) is correctly overloaded, and if not a compiler error is returned as shown in the examples above.

What about method overriding?

Overriding methods is completely different from overloading methods. If a derived class requires a different definition for an inherited method, then that method can be redefined in the derived class. This would be considered overriding. An overridden method would have the exact same method name, return type, number of parameters, and types of parameters as the method in the parent class, and the only difference would be the definition of the method.

Example of method overriding

Let’s go through a simple example to illustrate what method overriding would look like:

public class Parent {

 public int someMethod() {
   
   return 3;
       
    }
}

public class Child extends Parent{

 // this is method overriding:
 public int someMethod() {

    return 4;
       
    }

}

In the sample code above, someMethod is an overridden method in the Child class, because it has the exact same name, number of parameters, and return type as the someMethod method defined inside it’s parent class (conveniently named Parent).

Another important point to remember is that overriding is a run time phenomenon – not a compile time phenomenon like method overloading.

Summary of differences between overloading and overriding

Let’s summarize the differences between overloading and overriding.

When overloading, one must change either the type or the number of parameters for a method that belongs to the same class. Overriding means that a method inherited from a parent class will be changed.

But, when overriding a method everything remains exactly the same except the method definition – basically what the method does is changed slightly to fit in with the needs of the child class. But, the method name, the number and types of parameters, and the return type will all remain the same.

And, method overriding is a run-time phenomenon that is the driving force behind polymorphism. However, method overloading is a compile-time phenomenon.