In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.
The following Bicycle class is one possible implementation of a bicycle:
class Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
void changeCadence(int newValue) {
cadence = newValue;
}
void changeGear(int newValue) {
gear = newValue;
}
void speedUp(int increment) {
speed = speed + increment;
}
void applyBrakes(int decrement) {
speed = speed - decrement;
}
void printStates() {
System.out.println("cadence:"+cadence+" speed:"+speed+" gear:"+gear);
}
}
The syntax of the Java programming language will look new to you, but the design of this class is based on the previous discussion of bicycle objects. The fields cadence, speed, and gear represent the object's state, and the methods (changeCadence, changeGear, speedUp etc.) define its interaction with the outside world.
You may have noticed that the Bicycle class does not contain a main method. That's because it's not a complete application; it's just the blueprint for bicycles that might be used in an application. The responsibility of creating and using new Bicycle objects belongs to some other class in your application.
In OOP, a class defines a category of object. A class describes the properties (data) and methods (behaviors) for an object, much like an architectural blueprint describes the characteristics of a building. You write a custom class in an external file (header files, etc.) and you can add it into your application project.
Classes can be very useful when you build larger applications because you can organize a lot of the application's complexity in classes. When you move a lot of the logic into a custom class, you can not only make the code easier to reuse, but you can also "hide" some of the methods and properties from other parts of the code. This helps you prevent people from accessing sensitive information or changing data that shouldn't be changed.
When you use a class, you can also extend existing classes and add new functionality or modify existing functionality. For example, if you create three very similar classes, you can write a base class and then write two other classes that extend the base class. These two classes can add additional methods and properties, so that you don't need to create three classes that all duplicate the same code and logic.
Another benefit of using classes is code reusability. For example, if you create a custom class that creates a custom progress bar using the Drawing application programming interface (API), you could save the progress bar class in your classpath and reuse the same code in all of your applications by importing the custom class.