Object Oriented Programming
Class Declaration in Java
Classes are a blueprint for creating individual objects that contain the general characteristics of a defined object type. A modifier may or may not be used to declare a class.
Syntax
modifier class myClass { //class header
//field, constructor
//method declarations
};
Notes
The class body contains constructors for initializing new objects, declarations for the fields that provide the state of the class and its objects, and methods to implement the behaviour of the class and its objects.
Example
public class Bike {
//fields:
public int speed;
public int gear;
//Constructor:
public Bike(int startSpeed, int startGear) {
this.gear = startGear;
this.speed = startSpeed;
}
//Methods:
public void setGear (int newValue) {
this.gear = newValue;
}
public void applyBrake(int decrement) {
this.speed -= decrement;
}
public void speedUp(int increment) {
this.speed += increment;
}
}