6.2 Designing a class

For example, suppose that you want to design the class Rectangle that implements the basic properties of a Rectangle. Now every Rectangle has a width and height, which can be a floating-point value. Therefore, when you created an object of the class Rectangle, then you must store the length and width of the Rectangle into that object. Next, the two basic operations that are performed on a Rectangle are to find the area and perimeter of the Rectangle. Thus, the class Rectangle must provide these two operations. This class needs to provide a few other operations to effectively use this class in a program. In skeleton form, the definition of the class Rectangle looks as follows:

public class Rectangle
{
   private double length;
   private double width;

   public double getArea()
   {
      // code to determine the area of the Rectangle
   }

   public double getPerimeter()
   {
      // code to determine the perimeter of the Rectangle
   }

   // Additional methods as needed ...
}

In the definition of the class Rectangle, the variable length and width is declared with the keyword private and the methods area and perimeter are declared with the keyword public.When a member of a class is declared private. It can be used only by the methods of that class. The public access modifier permits a member to be accessed by code outside the class.

Here is complete class example : (Rectangle.java)

/**
 * The Rectangle class stores and manipulates
 * data for a rectangle.
 */
public class Rectangle
{
   private double length;
   private double width;

   /**
    * The set method accepts two arguments
    * which are stored in the length and width fields
    */
   public void set(double l, double w)
   {
      length = l;
      width  = w;
   }

   /**
    * The getArea method computes and returns the area
    */
   public double getArea()
   {
      return length * width;
   }

   /**
    * The getPerimeter method computes and returns the perimter
    */
   public double getPerimeter()
   {
      return 2 * (length + width);
   }
}

Variable Declaration and Object Instantiation

Once a class is defined, you can declare reference variables of that class type. For example, the following statement declare rectangle1 to be reference variables of type Rectangle

Rectangle rectangle1;

To store the length and width, we need to create a Rectangle object, which is accomplished by using the operator new :

rectangle1 = new Rectangle();

You can combine variable declaration and object instantiation in one statement

Rectangle rectangle 2 = new Rectangle();

 

reference-variable-in-java

 

Accessing Class Members

Once an object of a class is created, the object can access the members of the class using dot . operator :

rectangle1.set(4.1,6.3)

(RectangleDemo. Java)

/**
 * This program test the Rectangle
 * class methods.
 */
public class RectangleDemo
{
   public static void main(String[] args)
   {
      // Create a Rectangle object
      Rectangle rectangle1 = new Rectangle();

      // Accessing class members
      rectangle1.set(4.2, 6.2);
      System.out.println("Area of Rectangle is "
                         + rectangle1.getArea());
      System.out.println("Perimeter of Rectangle is "
                         + rectangle1.getPerimeter());
   }
}

Output :

Area of Rectangle is 26.04
Perimeter of Rectangle is 20.8