Class Design

In our car example on the previous objects page we look at some of the information that we could represent about all cars. We continue this example to create a class that we can use to store information about a car.

To create an object class we begin by create a new Java class with an appropriate name, in our case “Car”, that begins with Capital letter and does not include any spaces.

We can think of an object class as a plan or design on paper which will be used to create or build something. Similar to how a car would be designed on paper and then actually built in factory, a class is coded and then used to create objects in application or driver classes. This is called instantiation.

Instantiation is the concept of creating an object from a class or “plan” of the object. Many objects can be created from one class.

A Java class must always be saved as a filename with the same name of the class so our Car class would need to be saved asCar.java.

Our initial Car class will look like so:

public class Car
{

}

At the moment this car class is not very useful because it does not contain anything that will allow use to store or manipulate any information about cars so we need to now add attributes. Attributes are variables that a declared inside a class to store information about an object. For a car class we may want to include attributes such as the make of car, the car’s engine capacity and the odometer reading. We would declare variables inside the car class to store these as follows:

public class Car
{
    private String make = "";
    private int engineCap = 0;
    private int odometer = 0;
}

Our car class now has the ability to store information about itself but it does not have any functionality such as being able to create itself or to be able to use or change the attributes it contains.

In order for a car to be created or instantiated it needs to include a constructor. A constructor is a special piece of code that tells an object how it should be created. It could be compared idea of a Car being able to build itself or a house being able to construct itself from a plan because it has the functionality and ability to do so. If we add a contructor to our class it will look like so:

public class Car
{
    private String make = "";
    private int engineCap = 0;
    private int odometer = 0;

    public Car()
    {

    }
}

The contructor that has been added above is called a default constructor because it includes no information about what information the car should have when it is created. We can see this because there are no parameters in the round brackets ( ) after Car and there are no values being set inside the braces { } of the constructor.

Classes also normally include a parameterised constructor which is a constructor that accepts values for the attributes of the class in the round brackets ( ). These parameters are declared as they are brought into the constructor as shown below.

public class Car
{
    private String make = "";
    private int engineCap = 0;
    private int odometer = 0;

    public Car() // default constructor
    {

    }

    public Car(String makeIn, int engineCapIn, int odometerIn)
    {
        setMake(makeIn);
        setEngineCap(engineCapIn);
        setOdometer(odometerIn);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*