Classes (The Java™ Tutorials Learning the Java Language (original) (raw)
The introduction to object-oriented concepts in the lesson titled Object-oriented Programming Concepts used a bicycle class as an example, with racing bikes, mountain bikes, and tandem bikes as subclasses. Here is sample code for a possible implementation of a Bicycle
class, to give you an overview of a class declaration. Subsequent sections of this lesson will back up and explain class declarations step by step. For the moment, don't concern yourself with the details.
public class Bicycle {
// **the Bicycle class has**
// **three _fields_**
public int cadence;
public int gear;
public int speed;
// **the Bicycle class has**
// **one _constructor_**
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
// **the Bicycle class has**
// **four _methods_**
public void setCadence(int newValue) {
cadence = newValue;
}
public void setGear(int newValue) {
gear = newValue;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}
A class declaration for a MountainBike
class that is a subclass of Bicycle
might look like this:
public class MountainBike extends Bicycle {
// **the MountainBike subclass has**
// **one _field_**
public int seatHeight;
// **the MountainBike subclass has**
// **one _constructor_**
public MountainBike(int startHeight, int startCadence,
int startSpeed, int startGear) {
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}
// **the MountainBike subclass has**
// **one _method_**
public void setHeight(int newValue) {
seatHeight = newValue;
}
}
MountainBike
inherits all the fields and methods of Bicycle
and adds the field seatHeight
and a method to set it (mountain bikes have seats that can be moved up and down as the terrain demands).