Classes

Classes are blueprints for objects and therefore used for the creation of objects. Here is sample code for a possible implementation of a Bicycle class:

public class Bicycle {

    // three fields
    private int gear;
    private int speed;

    // one constructor
    public Bicycle(int startSpeed, int startGear) {
        gear = startGear;
        speed = startSpeed;
    }

    // methods
    public int getGear() {
        return gear;
    }

    public void setGear(int newGear) {
        gear = newGear;
    }

    public void applyBrake(int decrement) {
        speed -= decrement;
    }

    public void speedUp(int increment) {
        speed += increment;
    }   
}

Member Variables

Member variables, also called instance variables or fields are variables which are used to store states of objects.

The Bicycle class uses the following lines of code to define its fields:

private int gear;
private int speed;

Field declarations are composed of three components, in order:

  • Zero or more modifiers, such as public or private.
  • The field's type.
  • The field's name.

The fields of Bicycle are named gear, and speed and are all of data type integer (int). The private keyword identifies these fields as private members, accessible only by the Bicycle class.

Methods

Every class can have its own methods to express class specific behaviour. If a method is declared without the keyword "static" a instance of this class is needed in order to invoke the method. This is a method which decreases the speed of an instance of the type Bicycle:

public void applyBrake(int decrement) {
    speed -= decrement;
}

Constructors

A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations, except that they use the name of the class and have no return type. For example, Bicycle has one constructor:

public Bicycle(int startSpeed, int startGear) {
    gear = startGear;
    speed = startSpeed;
}

To create a new Bicycle object called myBike, a constructor is called by the new operator which creates space in memory for the object and initializes its fields:

Bicycle myBike = new Bicycle(0, 8);

Although Bicycle only has one constructor, it could have others, including a no-argument constructor:

public Bicycle() {
    gear = 1;
    speed = 0;
}

Access Modifiers

The first (left-most) modifier used in declaring fields and method inside classes lets you control what other classes have access to a member field or a method:

  • public modifier: the field/ method is accessible from all classes.
  • private modifier: the field/ method is accessible only within its own class.
  • protected and default: not relevant at the moment

It is common to make fields private. This means that they can only be directly accessed from the Bicycle class. We still need access to these values, however. This can be done indirectly by adding public methods that obtain the field values for us:

public int getGear() {
    return gear;
}

Source:

results matching ""

    No results matching ""