Method Overloading:
The java programming language supports overloading methods, and java can distinguish between methods with different method signatures.This means that methods within a class can have the same name if they have different parameter lists.
Example:
import java.io.*;
import java.lang.*;
import java.util.*;
class OverloadingDemo
{
public void area()
{
Scanner s = new Scanner(System.in);
int w,l,area;
System.out.println("o*******o********welcome********o**********o");
System.out.println("Enter the value of width and length :\t");
w = s.nextInt();
l = s.nextInt();
area = w*l;
System.out.println("Area of Rectangle :\t"+area);
}
public int area(int height,int base)
{
int area = (height*base)/2;
return area;
}
public void area(float r)
{
final float Pi = 3.14159f;
//Declaration of float constant
float area;
area=Pi*r*r;
System.out.println("Area of circle : \t"+area);
}
}
class TestDemo
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
/*
Scanner class is used for taking input from user
*/
OverloadingDemo obj=new OverloadingDemo();
int height,base,area;
float radius;
obj.area();
//calling a without arguments and without return value method
System.out.println("Enter the value of height and base:\t");
height=s.nextInt();
base=s.nextInt();
area=obj.area(height,base);
System.out.println("Area of triangle :\t"+area);
System.out.println("Enter the value of radius:\t");
radius=s.nextFloat();
obj.area(radius);
}
}
Expected output :
The java programming language supports overloading methods, and java can distinguish between methods with different method signatures.This means that methods within a class can have the same name if they have different parameter lists.
Example:
import java.io.*;
import java.lang.*;
import java.util.*;
class OverloadingDemo
{
public void area()
{
Scanner s = new Scanner(System.in);
int w,l,area;
System.out.println("o*******o********welcome********o**********o");
System.out.println("Enter the value of width and length :\t");
w = s.nextInt();
l = s.nextInt();
area = w*l;
System.out.println("Area of Rectangle :\t"+area);
}
public int area(int height,int base)
{
int area = (height*base)/2;
return area;
}
public void area(float r)
{
final float Pi = 3.14159f;
//Declaration of float constant
float area;
area=Pi*r*r;
System.out.println("Area of circle : \t"+area);
}
}
class TestDemo
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
/*
Scanner class is used for taking input from user
*/
OverloadingDemo obj=new OverloadingDemo();
int height,base,area;
float radius;
obj.area();
//calling a without arguments and without return value method
System.out.println("Enter the value of height and base:\t");
height=s.nextInt();
base=s.nextInt();
area=obj.area(height,base);
System.out.println("Area of triangle :\t"+area);
System.out.println("Enter the value of radius:\t");
radius=s.nextFloat();
obj.area(radius);
}
}
Expected output :
0 Comments