Description
This program demonstrates constructor overloading in Java. A constructor is a function with the same name as that of the class in which it is defined. Constructor overloading refers to the use of multiple constructors in the same class with different parameters. This program implements two constructors, one with a single parameter and the other with two parameters.
//Java program to demonstrate constructor overloading
//Java constructor Overloading to find the area of circle and rectangle
//How to implement constructor overloading in Java
import java.io.*;
class Area
{
double arc,arr;
void Area(double p)
{
arc=(2)*(3.14)*(p);
System.out.println("Area is "+arc);
}
void Area(double x,double y)
{
arr=2*x*y;
System.out.println("Area is "+arr);
}
public static void main(String s[]) throws Exception
{
Area acl=new Area();
int ar;
double ac,r,l,b;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Area of a circle");
System.out.println("Enter the value for r");
r=Double.parseDouble(br.readLine());
acl.Area(r);
System.out.println("Area of a rectangle");
System.out.println("Enter the value for l and b");
l=Double.parseDouble(br.readLine());
b=Double.parseDouble(br.readLine());
acl.Area(l,b);
}
}
Output
Java Constructor Overloading |
No comments:
Post a Comment