Friday, 13 June 2014

OVERLOADING

OVERLOADING :

Using any entity in more than a single way in programming is called overloading . Overloading is possible in c++ as well as JAVA .

In general , every programming language provides  two types of overloading :

OPERATOR OVERLOADING :

Operator overloading is the criteria of using the same method name for different operations . An operator for different data types give output accordingly . Consider the following example :


In JAVA , operator overloading is not possible but it uses internal operator overloading .
It means that user cannot defined operator overloading , but for some cases operators are already overloaded in JAVA . They are already defined in in-built classes.

METHODS OVERLOADING :

In method overloading , more than one methods have same name but they are used for different purposes .
Name of methods is name but they perform different operations and are used in different situations .

Defining and declaring methods with same name :
In method overloading name is same but number of arguments or type of arguments must be different.

Note that method overloading does not depend upon return type of method.

A program to illustrate method overloading is given :


class Mover
{
int a,b;
double d,c ;


void get(int a)
{
this.a=a;
b=a;
}
void get(int a,int b)
{
this.a=a;
this.b=b;
}
int area()
{
return(a*b);
}

public static void main(String args[])
{
Mover obj = new Mover();
obj.get(10);
System.out.println("Area of square "+obj.area());
obj.get(15,30);
System.out.println("Area of rectangle "+obj.area());
}
}



The output of this program will appear like this :




0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes