For someone who is familiar with "C" programming language, two functions (equivalent to methods in Java) in C cannot have the same name (at-least in the same .c file). This leads to a disadvantage where, one can end up defining more than one function for a slight change in the type it returns or argument type or argument count.

For example, if we were to an add function in C, then we would need to define different combinations of the input types to cater to different variable types.

 void add_2_int (int x, int y);
 void add_3_int (int x, int y, int z);
 void add_3_float (float x, float y, float z);
 void add_2_float (float x, float y);

If you notice the above functions, you would see that all of them do the same thing; addition. In this case, It makes more sense to have one function with name "add" and let the compiler take care, what version of "add" function to call. Unfortunately C does not allow this. This concept of having the same name for more than one method is called "overloading". One of the ways of achieving Polymorphism (which means "many forms") in Java, is through overloading.

Method Overloading

Java supports method overloading and is a pretty useful feature. When an overloaded method is called, Java compares the methods signature (Signature is nothing but, methods return type, argument list, argument type) and calls the appropriate overloaded method. Let us apply the concept of method overloading and redefine the above function in Java. Since we can use overloading, we can now use the same name for all of them!

 void add (int x, int y);
 void add (int x, int y, int z);
 void add (float x, float y, float z);
 void add (float x, float y);
 float add (float x, float y);
 int add (int x, int y);

 In fact, we can define many combinations of the add function.

Here is an example program that demonstrates the concept of "overloading".

 class Overloading {
     void add (int x, int y, int z) {
 	int sum = x + y + z;
 	System.out.println (" Integer Sum : " + sum);

     double add (double x, double y) {
 	return x + y;

 public class OverloadingExample {
     public static void main(String[] args) {
 	int var1 = 4, var2 = 3, var3 = 5;
 	double d1 = 4.5, d2 = 5.6;
 	double sum;

 	Overloading obj1 = new Overloading();
 	obj1.add(var1, var2, var3);
 	sum = obj1.add(d1,d2);
 	System.out.println (" Double Sum  : " + sum);

Compile/Run this program and here is the output:

  Integer Sum : 12
  Double Sum  : 10.1

Constructor Overloading

As we have seen earlier, constructors follow the same semantics of a method, they are also methods after all! Hence, Like method overloading we also have the concept of constructor overloading. We could overload constructor as many times as we want, constructor with 1 argument, constructor with 2 args and so on. Here is an example program that demonstrates the concept of constructor overloading.

Abstract class and overriding

Abstract Class and overriding are 2 very powerful concepts of OOP, we will see more about these in the coming sections. Overriding refers to a concept where, a method in the sub-class/sub-classes will have the same name and also the signature, as that of a method in its super class. Overriding unlike overloading allows us to have 2 or more methods with the same name and also the signature.

comments powered by Disqus