Calculate area of rectangle using default constructor in java

Let’s learn 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 – major features of java

Now let’s see java program to find area of rectangle using 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