Calculate area of rectangle using default constructor in java

Hey guys!! Welcome to flower brackets blog. In this post we are going to calculate area of rectangle using default constructor in java.

calculate area of rectangle using default constructor in java

Calculate area of rectangle using default constructor in java

A default constructor is one which takes no arguments. Java runtime creates one if a class does not explicitly define any constructors.

Also read – Learn constructor in java

Now let’s see java program on area of rectangle using default constructor,

class RectangleShape 
{
int area, length, breadth;
RectangleShape()
{
length = 50;
breadth = 20;
}
void getArea()
{
area = length * breadth;
System.out.println("Area of Rectangle : " + area);
}
};
public class RectangleAreaConstructor
{
public static void main(String[] args)
{
RectangleShape rs = new RectangleShape();
rs.getArea();
}
}

Output:

Area of Rectangle : 1000

Related Posts