Method Overloading :
Method Overloading is an example of Polymorphism.As the name "method overloading" suggests that a method is overloaded to do more than one task. In java, we can define more than one methods within the same class that shares a common name. The only way to distinguish between them is by looking at the numbers and types of parameters passed into it.
example:
Class ShapeArea{
int length, width, side;
float radius;
public static void area(int x , int y){
length = x;
width = y;
int area1= length*width;
System.out.println("length =" + length +"width =" + width);
System.out.print("Area of the rectangle = " + area1);
}
public static void area(int z){
side = z;
int area2= side*side;
System.out.println("side =" + side);
System.out.print("Area of the square = " + area2);
}
public static void area(float r){
radius = r;
float area3 = 3.14*radius*radius;
System.out.println("radius =" + radius);
System.out.print("Area of the circle = " + area3);
}
}
Class AreaDemo{
ShapeArea a1 = new ShapeArea();
ShapeArea a2 = new ShapeArea();
ShapeArea a3 = new ShapeArea();
public static void main(String args[]){
a1.area(5,6);
a2.area(4);
a3.shape(2.5);
System.out.println("Thank you!! ");
}
explanation :
.In the above program , we can see that there are three methods defined within the ShapeArea class , and they share the same name "area" .At compile time the java compiler decides which method is being called by checking the number of arguments and the types of arguments passed into it.
above we can see that in main class we hve called the area method thrice. The first two i.e "a1.area(5,6)" and "a2.area(4), they both have the same integer type arguments but the number of arguments are different. So when the a2.area(4) is called the compiler refers to the method in which area of the square is written,as the parameters set for the area of square matches with that of the arguments. Now , the method defined for the area of circle and square , have same number of parameters but the types of parameter are different.So when area(2.5) , the compiler calls the area method (circle), as the argument passed is of float type.
Constructor Overloading
The theory behind constructor overloading is very much similar to method overloading, here we will deal with constructor only.
Class area{
int length, width;
area(int x , int y){
length = x;
width = y;
}
area(int z){
length = z;
width =z;
}
public static void CalArea(){
return length*width;
}
}
Class AreaDemo{
public static void main(String args[]){
area a1 = new area(5,6);
area a2 = new area(4);
int p ;
p = a1.CalArea();
System.out.print("area of rectangle =" + a1);
p = a2.CalArea();
System.out.println("area of square =" + a2);
System.out.println("Thank you!! ");
}
}
expaination:
Here area is the constructor, and we have set parameters for both rectangle and square .The constructor for square will take only one integer type argument , whereas rectangle will take two integer type arguments. We are calling them by creating objects a1 and a2 for rectangle and square respectively.
Object as Parameter
Till now we have used various data types as a parameter.It is not compulsory that data types can only be the parameters for our methods or constructor. We can set objects as our parameter.The following program can solve your query that how we can use object as a parameter.
Class Dimension{
Dimension{
int length;
int width;
Dimension(Dimension Ob){
length = ob.length;
width = ob.width;
}
Dimension(int x,int y){
length = x;
width =y;
}
Dimension(int s){
length = s;
width =s;
}
public static void CalArea(){
return length*width;
}
}
Class ParameterOb{
public static void main(){
Dimension a1 = new Dimension(5,6);
Dimension a2 = new Dimension(4);
Dimension copy = new Dimension(a2);
int Area1,Area2,Area3 ;
Area1 = a1.CalArea();
System.out.println("area of rectangle =" + Area1);
Area2 = a2.CalArea();
System.out.println("area of rectangle =" + Area2);
Area3 = copy.CalArea();
System.out.println("area of rectangle =" + Area3);
System.out.println("Thank You !!);
}
}
Arguments Passing
In java passing arguments in a subroutine(subroutine = It is a portion of code within a larger program that performs a specific task ) is very much similar to what we did in C. It is done in two ways , first way is
call-by-value.In this approach copies the value of the argument into the formal parameters of a subroutine.Therefore, changes made to the parameters have no effects on the arguments.The second way is
call-by-reference.In this approach , a reference to an argument is passed to the parameter.Inside the subroutine, this argument is used to access the actual argument specified in the call.
This means the change in parameter will affect the arguments used to call the subroutine.
//example of call-by-values
class DEmo{
void change(int x, inty){
i = i+j;
j = j*i;
}
System.out.println(thank you!!");
}
class show{
public static void main(){
DEmo b = new DEmo();
int x = 5, y =6;
System.out.println("x and y before and after change " + x +" " + y);
b.DEmo(5,6);
System.out.println("after change x and y are " + x + " " + y + " respectively" );
}
}
//example of call-by-reference
class test{
int a, b;
test(int i , int j){
a = i;
b = j;
}
//pass an object
void meth(test o){
o.a += 2;
o.b /= 2;
}
}
class Callbyreference{
public static void main(){
test ob = new test(15,20);
System.out.println("ob.a and ob.b before call: " + ob.a + " " + ob.b );
ob.meth(ob);
System.out.println("ob.a and ob.b after call : " + ob.a + " " + ob.b );
}
}
output:
ob.a and ob.b before call: 15 20
ob.a and ob.b after call: 30 40
explanation :
We can see, in this case , the action inside meth() have affected the object used as an argument.
When an object reference is passed to a method , the reference is passed by use of call-by-value.However, since the value being passed refers to an object , the copy of that value will still refer to the same object that its corresponding argument does.
Returning an object
Theory is simple here, the method we will define returns an object of the class type.
//returning an object
class test{
int x;
test(int i){
x = i;
}
test incrbyten(){
test temp = new test(a + 10);
return temp;
}
class returnob{
public static void main(String args[]){
test ob1 = new test(2);
test ob2;
ob2 = ob1.incrbyten();
System.out.println("ob.a : " + ob1.a);
System.out.println("ob.2 : " + ob2.a);
ob2 = ob2.incrbyten();
System.out.println("ob2.a after second increase : " + ob2.a);
}
}
output:
ob1.a : 2
ob2.a : 12
explanation :
We can see , each time we call the inrbyten() method is invoked , a new object is created , and a reference to it is returned to the calling routine.Here we are using the new operator for allocating memory at run time, so we don't need to worry about the object going out of scope because the method keeps executing till the refernce is avaiilable to it.If there is no reference given then the object will be reclaimed by the garbage collector.
STATIC
Till now we were knowing that an object is the only way to communicate with a class members.But in java, we can communicate with a class member without creating an object.We can do this with the help of "static" keyword.When a class member is declared as static, it can be accessed before any objects of that class are created, and without reference to any object.The most common example of a static function is the main() function. main() is declared as static because it must be called efore any object exist.
Instance variables declared as static are, essentially , global variables.When objects of that class are declared, no copy of the static variable is made.Instead , all instance of the class share the same static variable.
Methods declared as static have several restriction :
* they can only call other static data.
* they must only access static data.
* they cannot refer to this and super in any way.
(
this keyword is always a reference to an object on which the method was invoked.
super keyword is used for the superclass objects or constructor)
FINAL
A variable or a method can be declared as final.final is a keyword which helps us to declare a variable which cannot be modified further.It does not occupy any memory space per instance basis.Thus, it can be considered as a constant. However, a final method and final varables are not same, final method has different meaning which we will discuss latter in inheritance part.