2. Why learn Overloading?
‘Overloading’ is one of the crucial and fundamental
concepts of core Java
The concept of ‘Overloading’ promotes clean Java coding
3. Example of Overloading
public int operator(int a, int b){
return a+b;}
public float operator(float a, float b){
return a+b;
}
Note the different
argument lists – first
method takes integers
as its argument
The second method
takes floating type
variables as its
argument
4. Facts of Overloading
Only methods in Java can be overloaded
Methods can be overloaded in the same class or sub-class
Each overloaded method is considered to be a separate
method
5. Rules for Overloading
In order for the method to be considered overloaded, the
argument list MUST be different
Just changing the return type and access modifier of a
method is not enough to make it overloaded
6. Invoking Overloaded methods
As an example, when the compiler sees
operator(5, 3);
it calls this method
public int operator(int a, int b){
return a+b;
}
7. Overloaded methods can
have a different return type
have a different access
modifier(can be more restrictive)
throw new or broader
exceptions BUT MUST
differ in the argument list
Summary
Editor's Notes
Hello everyone…I am Jayanthi and today’s video will focus on Overloading in Java.
Next, we will see what is meant by overloading. Overloading as the name suggests is giving the same to different methods but achieving different functionality by providing different argument lists, return type or access modifier. Here we see two methods which are overloaded – they are
Public int area(int a, int b) and
Public int area(int a, int b, int c)
Both of these methods are used to find the area – but one takes two integers as argument and the other takes three integers as its argument. It must be noted that unlike other programming languages Java only supports method overloading and not operator overloading
Next, we will see what is meant by overloading. Overloading as the name suggests is giving the same to different methods but achieving different functionality by providing different argument lists, return type or access modifier. Here we see two methods which are overloaded – they are
Public int area(int a, int b) and
Public int area(int a, int b, int c)
Both of these methods are used to find the area – but one takes two integers as argument and the other takes three integers as its argument. It must be noted that unlike other programming languages Java only supports method overloading and not operator overloading
Next, we present some rules for overloading. This will be help us in creating effective overloaded methods in our programs. The most important rule is that a method is considered overloaded only if the argument list is different(both number and type of arguments). For example, the area method showed one method having two integers as arguments while the other method showed three integers as arguments.
Changing the return type and access modifier do not make a method overloaded. It is also important to note that each overloaded method is a separate method.
Next we deal with the question of how overloaded methods are invoked. How does the compiler know which method to call when there are two overloaded methods with the same name? The compiler will decide this based on the argument lists. If the method with integer argument is invoked, then the compiler calls the method with integer arguments. Similarly, if the method with floating arguments is invoked, then the compiler calls the appropriate method with floating type arguments.
We see this in our example, when the compiler sees
Operator(3,5) in our code, then the compiler immediately calls the operator method having two integers as its argument. When the compiler sees operator(1.2,2.3), it immediately calls the operator method having two floating point variables as its argument. We will now move onto a demonstration of this concept.