In my previous post I have
talk about Methods & Constructors in Java. In this post of mine I
will detail on topics, method & constructor overloading and argument
passing in Java. If you are a beginner to Java and your search has landed you
directly into this page and you think that you have to learn at little more
about classes in java before proceeding with this chapter. If so kindly visit
the Class of Java page. You want to learn more basics about Java kindly
go to bottom of this page there you can find links to my previous pages on
basics of java.
Method Overloading:
In Java, it is possible for you to
define more than one methods within the same class with same name until one
condition is satisfied i.e. all these method have different parameter
declaration. This kind of method defining in Java is called method overloading.
Before proceeding with
method overloading we must know what is the difference between parameters and
arguments?
Parameters are the formal variables defined with
the method. Below code fragment shows you example of parameters.
int Area(int length, int breadth) //parameters
{
Return
length*breadth;
}
Arguments are the values passed while calling
any method. Below code fragment shows you example of arguments.
Object1.Area(10,20);//Arguments
Ok! Back to
method overloading.
When an overloaded
method is called by any code, then Java decides which version of the overloaded
method to be called, depending on the types of arguments or the number of
arguments passed during the call to a overloaded method.
Note: Method
overloading is basically defining more than one method with the same name
within the same class and these methods have different parameter declaration,
parameters either differ by type or by numbers.
You can see below program
example on method overloading.
package sample.example1627;
public class SampleOverloadingDemo
{
public static void main(String[] args)
{
SampleMethodOverloading smp1=new SampleMethodOverloading();
smp1.addition();
smp1.addition(20, 30);
smp1.addition(30.60, 14.56);
smp1.addition(88);
}
}
package sample.example1627;
public class SampleMethodOverloading
{
void addition()
{
int a=10;
int b=20;
System.out.println("addition in no
parameter method "+(a+b));
}
void addition(int a,int b)
{
int m=a+b;
System.out.println("addition in
parameter method "+m);
}
void addition(double a,double b)
{
System.out.println("addition in double
parameter method "+(a+b));
}
void addition(int a)
{
a+=10;
System.out.println("addition in addition
assignment parameter method "+a);
}
}
Output:
addition in no parameter method 30
addition in parameter method 50
addition in double parameter method
45.160000000000004
addition in addition assignment parameter method 98
In the above program example you can see three
different versions of addition methods and their respective outputs are shown.
Suppose think that you are writing this same program using a language which
will not support method overloading then, you would have to give different
names for all the above addition method versions. There is working for you to
remember the names of all these methods whenever you need to invoke them again.
Now I guess you can conclude how java has helped us with method overloading.
Constructor Overloading:
I have mentioned in my
previous post, Constructors & Methods that you can have more than
one constructor in a class. Constructor overloading is same as that of method
overloading. You can define more than one constructor within a class with
different parameter declaration i.e. parameters may either differ in their
types or numbers.
Below is
program example implementing constructor overloading.
package sample.example1627;
public class Rectangle
{
double length;
double breadth;
Rectangle()
{
length=10;
breadth=20;
}
Rectangle(double length,double breadth)
{
this.length=length;
this.breadth=breadth;
}
double Area()
{
return length*breadth;
}
}
package sample.example1627;
public class WorkingRectangle
{
public static void main(String[] args)
{
Rectangle rectangle1=new Rectangle();// object created
Rectangle
rectangle2=new Rectangle(16.27, 27.16);// object created
System.out.println("Area of rectangle
object1 "+rectangle1.Area());
System.out.println("Area of rectangle
object2 "+rectangle2.Area());
}
}
Output:
Area of rectangle object1 200.0
Area of rectangle object2
441.8932
In above example you have seen
that class rectangle has two constructors one without parameters and other with
parameters. Both of these constructors initialize the objects of the class when
they are created.
Argument Passing in Java:
If
you are a C/C++ programmer then you will be familiar with two ways in which
arguments are passed to the subroutine i.e. call-by-value and
call-by-reference.
Before discussing how
arguments are passed in Java? We will see what is call-by-value and
call-by-reference?
Call-by-value
is a way of argument passing in which passed argument is copied into formal
parameters of subroutine. Therefore in this approach arguments value are not
changed, even if parameter value have change after the execution of subroutine.
Call-by-reference is a way in which reference of an
argument is passed to the subroutine. In this case formal parameters will have
the reference to the actual arguments and after execution if value of parameter
is changed than the same change is reflected in the argument value also.
Now back to
Java, in Java call-by-value is used to
pass primitive types to a method and call-by-reference is used to pass objects
to a method.
Kindly go
through the program example below you will get a brighter idea of the concept
how and when java implements call-by-value.
package sample.example1627;
public class SampleMethodOverloading
{
int assign(int a,int b)
{
a=40;
b=30;
return a+b;
}
}
package sample.example1627;
public class SampleOverloadingDemo
{
public static void main(String[] args)
{
int valreturned;
int a=10,b=20;
SampleMethodOverloading smp1=new SampleMethodOverloading();
//values of arguments before execution.
System.out.println("values of
arguments before method execution "+a+" "+b);
valreturned=smp1.assign(a, b);//method calling
//values of arguments after execution.
System.out.println("values of
arguments after method execution "+a+" "+b);
System.out.println("value returned by
method "+valreturned);
}
}
Output:
values of arguments before method execution 10 20
values of arguments after method execution 10 20
value returned by method 70
In
the above program example you can see primitive type arguments are passed to
the method. Here arguments are passed by call-by-value. You can see value of argument
before execution and after execution is one and the same. And the value
returned by method is addition of 40 and 30 not 10 and 20. So it’s clear that
java uses call-by-value for passing primitive types to methods.
Kindly go
through the program example below you will get a brighter idea of the concept
how and when java implements call-by-reference.
package sample.example1627;
public class Rectangle
{
double length;
double breadth;
void Area(Rectangle ob)
{
ob.length+=20;
ob.breadth+=20;
System.out.println("Area "+(ob.length*ob.breadth));
}
}
package sample.example1627;
public class WorkingRectangle
{
public static void main(String[] args)
{
Rectangle r1=new Rectangle();
r1.length=25;
r1.breadth=35;
//value of argument before execution
System.out.println("Value of argument
before execution "+r1.length+" "+r1.breadth);
r1.Area(r1);
// value of arguemnt
after execution
System.out.println("Value of argument
after execution "+r1.length+" "+r1.breadth);
}
}
Output:
Value of argument before execution 25.0 35.0
Area 2475.0
Value of argument after execution 45.0 55.0
In the
above program example you can see object is passed as the argument to the
method. Here values before the execution and after the execution are not the
same. You can see both values are different. This is because when object is
passed as an argument, then actually it is the reference of the object that is
passed to the method. So change in the value of parameters after method
execution will also change the actual value of arguments. So it is clear that
Java uses call-by-reference when object is passed as the argument to the
method.
Note: Method
overloading is the one way in which Java supports polymorphism.