A class in Java is a template that is used to create objects, and to define the properties and behaviors of those objects.
For example, a Car class could be used to create objects that represent individual cars. The Car class would specify the properties of a car, like its make, model, and color, as well as the behaviors, like accelerate, brake, and turn.
public class Car {
// Properties of the class…
private String make;
private String model;
private int year;
private String color;
// Constructor of the class…
public Car(String make, String model, int year, String color) {
this.make = make;
this.model = model;
this.year = year;
this.color = color;
}
// Methods of the class…
public void accelerate() {
System.out.println(“Vroom!”);
}
public void brake() {
System.out.println(“Screech!”);
}
public void turn(String direction) {
System.out.println(“Turning ” + direction + “.”);
}
}