Hello,
We have already discussed about Overriding concept in my previous
post. Now let us see how Overloading concept works and the benefits that we get
in using it. Before proceeding further and for better understand I suggest you
to refresh the overriding concept in my previous post.
Overloading – This word can be divided into OVER and LOAD. Yes,
we are gonna load a method overly.
But the question comes, how to load a method, right? I believe you guys knew it, just by adding
arguments to a method.
OVER-LOADED Methods
We already know that overriding concept uses 2 excellent
features of Java – Inheritance and Polymorphism. If you are not aware of this,
please read this before you proceed further. These 2 features are mandatory to
implement the Overriding concept.
But for overloading concept, only Polymorphism feature is
mandatory. Inheritance feature is optional. It allows us to reuse the same
method name with different arguments and optionally different return types.
public class AddDemo
{
// addition of 2 integers
public void add(int a , int b)
{
System.out.println("Add 2 Integers --> "+(a+b));
}
// addition of 2 decimals -- OVER-LOADED METHOD
public void add(double a, double b)
{
System.out.println("Add 2 Decimals --> "+(a+b));
}
// addition of 3 decimals -- OVER-LOADED METHOD
public void add(int a, int b, int c)
{
System.out.println("Add 3 Integers --> "+(a+b+c));
}
// addition of integer and decimal and returns a decimal -- OVER-LOADED METHOD
public double add(int a, double b)
{
System.out.println("Add Integr and Decimal and returns a decimal --> "+(a+b));
return a+b;
}
// <--- This method will throw error --->
// Error : Duplicate method add(int, int) in type AddDemo
public int add(int a , int b)
{
System.out.println("Add 2 Integers and returns an Integer --> "+(a+b));
return a+b;
}
// main method - entry point of a program
public static void main(String... args)
{
AddDemo aD = new AddDemo();
// declare & define variables
int a = 10;
int b = 20;
int c = 30;
double d = 1.5;
double e = 1.75;
// invoke the methods
aD.add(a, b);
aD.add(a, b, c);
aD.add(d, e);
double f = aD.add(a, e);
System.out.println("AddDemo.main().. F value --> "+f);
}
}
Above program is self-explanatory. I have method
add() which takes 2 integer primitives. And I need some more methods in the
same class which need to do addition. So I tried using the same method name but
with different arguments.
But you may be wondering how Java compiler will get
to know which add() should be invoked when all the methods in the class have
same name. The compiler will recognize the correct method by the number and
type of arguments that we pass while calling the method.
The point you need observe in the above program is
the method add(int a , int b) which returns an integer. That method gives a
compile time error saying “Duplicate method in the class”. Why so?
'Coz overloading is possible ONLY in below combinations,
- Change the number of arguments
- Change the type of arguments
- Change the number of arguments and their types.
Changing the return type of a method and not the type
of arguments is not allowed in the same class. Coz the compiler will not be able
to recognize a method by the return type.
Below methods are not allowed in the same class,
public void add(int a, int b) { }
public int add(int a, int b){ }
Beginners get confused easily to find a method if it has
been overridden or overloaded in a subclass. Try to recognize if a method is overridden or
overloaded in the below program.
//class AddDemo
public class AddDemo
{
// add method
public void add(int a, int b){ }
// subtract method
public void subtract(int a, int b){ }
}
class Demo extends AddDemo
{
// Overridden or Overloaded method ??
public void add(int a, double b){ }
}
add() in class Demo is overridden or overloaded ?
It is overloaded. Coz overridden method should have
same number and type of arguments. If you create an object to the Demo class,
it is capable to invoke add() which is in its class and also add() in AddDemo
class (parent class).
The overloading concept becomes much more
interesting when we use the Objects instead of primitives. See the below
program.
public class TestAnimal{
public void eat(Animal an)
{
System.out.println("Animal method invoked ::");
}
// Overloaded method
public void eat(Horse an)
{
System.out.println("Horse method invoked ::");
}
public static void main(String... args)
{
TestAnimal testObj = new TestAnimal();
Animal animalObj = new Animal();
Horse horseObj = new Horse();
// invoke the methods
testObj.eat(animalObj);
testObj.eat(horseObj);
}
}
//class Animal
class Animal
{
}
// class Horse - subclass of Animal
class Horse extends Animal
{
}
In the above program, it is obvious that the output
will be,
Animal method invoked :: Horse method invoked ::
But what if we use an Animal reference to a Horse object?
See below statement,
Animal animalRefToHorse = new Horse(); testObj.eat(animalRefToHorse);
which eat method will be invoked?
The method that
takes Animal object or Horse object?
To understand better, let us go back to Overriding
concept. In Overriding, at compile time the Java compiler assumes it as Animal
Object and at runtime it realizes that the object is of Horse type and hence it
invokes the method in the Horse class. That’s how the Runtime or Dynamic
Polymorphism works.
But in overloading concept, even if the compiler
realizes the object at runtime is of Horse type, it still consider as Animal
object coz its reference is of type Animal. Hence the above statement will
invoke the eat() method that takes Animal object and we get the output as,
Animal method invoked ::
Confused ??!! :D
To summarize the entire explanation, here are the straight
2 points that you should remember,
- Reference type decides which overloaded method need to be invoked.
- Object type decides which overridden method need to be invoked.
I have summarized the differences between Overriding
and Overloading in the below table.
Overloaded
Method
|
Overridden
Method
|
|
Arguments(s)
|
Must
change
|
Must
NOT change
|
Return
Type
|
Can
change
|
Must
NOT change. Can return sublcass Object
|
Exceptions
|
Can
Change
|
Reduce
or eliminate. Must not throw broader or new exceptions
|
Access
|
Can
change
|
Must
not be more restrictive. Can be same / less
|
Invocation
|
Based
on Reference Type
|
Based
on Object Type
|
Thanks :)
