❮ Previous Next ❯

Method Overloading

Example

class Overloading
{
void show( )
{
system.out.println("Tapuuu");
}
void show( int a)
{
system.out.println("a= "+a);
}
void show( int a, int b)
{
system.out.println("a and b="+a+" "+b);
}
void show(double a);
{
system.out.println("a= "+a);
}
}
class OverloadingDemo
{
public static void main(String[] args)
{
Overloading obj= new Overloading( );
obj.show( );
obj.show(15);
obj.show(10, 20);
obj.show(5.2);
}
}
OUTPUT

Tapuuu
a=15
a and b= 10 20
a=5.2

Here we have seen that, show()method is overloaded four times. The first method takes no parameter, the second method take one integer parameter, the third takes two integer parameters and the fourth takes one double parameter.Let us see another example :

wap to find out the area of triangle, rectangle and square using the concept of method overloading


class Area
{
void area(double base, double height)
{
double a;
a=0.5*base*height;
system.out.println("the area of triangle is:"+a);
}
void area(int a)
{
int arr;
arr=a*a;
system.out.println("the area of square i:"+arr);
}
void area(int length, int breadth)
{
int x;
x=length*breadth;
system.out.println("the area of rectangle i:"+x);
}
}
class AreaDemo
{
public static void main(String[] args)
{
Area obj= new Area();
obj.area(5.2, 3.4);
obj,area(5);
obj.area(10, 20);
}
}
Output

the area of triangle is 8.84
the area of square is 25
the area of rectangle is 200

❮ Previous Next ❯



  Tutorials

Popular Links

Contact Us

address Plot No-N6/530,2ND Floor
Nayapalli,Jayadev Vihar
      Bhubaneswar-751015
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved