5
Classes and Objects
As mentioned in the introduction, Java is an object-orientated programming language. Object-orientated programming is a paradigm that uses the concept of an object to encapsulate the data and behavior of a program. Objects are reusable modules, and can interact with one another as the program executes.
This differs from many other programming languages that use functions or procedures to encapsulate the behavior of the program: these languages are called functional-orientated and imperative languages respectively.
Object-orientated programming was already well established when Java was created, and can be traced all the way back to the 1960s. Java was notable for being a more pure object-orientated language than many of its direct competitors, however, most notably C++..
The most important concept to grasp in this chapter is the difference between a class and an object. A class acts as a template for objects: it declares the data that will be held by an object, and the methods that operate on this data. Put another way, once a class is defined, many instances of the class can be constructed, and these are called objects.
You can think of classes as molds. Once you have constructed your mold you can use it to create as many objects as you like from the mold.
Some object-orientated languages let you create objects without classes (yes, I am looking at you again JavaScript). With Java, however, you always need to start with a class.
Each object created from a class is independent from the other objects constructed from the same class, but the methods available remain the same. Before explaining the concepts of classes and objects in more detail it is worth doing so with examples.
Classes are usually thought of as the “nouns” of a program. You may remember from grammar class that nouns are used to name “things” such as people, places, ideas or animals. These are opposed to verbs, which describe actions.
When you first sit down to write a Java program you need to think about what the nouns will be. For instance, in a rental car program the nouns may be Customer, Car and Rental agreement. In a banking program the nouns may be Customer, Account and Branch.
Once you have defined the nouns, you next start thinking about the data each of these nouns needs to hold. You describe the data that will be captured by defining fields on the class. For instance, a Rental agreement class might contain fields that capture the daily rate, the date the agreement was signed, and any special charges that will be applied to the rate. The class defines the fields, while each object will be assigned specific values for these fields.
Finally you need to think about what behavior is required from the class; the behavior will be captured in the class’s methods. For instance, the Rental agreement class might have a method that calculates the total cost to the customer. In order to achieve this, methods can read and modify the data held by an object.
You will remember from the Hello World example that you declared a class called HelloWorld. Before beginning the examples in this chapter it is worth pointing out a quirk of the Hello World program. In that program you did not create an object from the HelloWorld class, yet its main method could still be invoked. This is because the method was declared as static:
public static void main(String[] arguments) {
Static methods are different from most of the methods that you will look at in this book because they can be invoked directly on the class, rather than instances of the class (its objects). This is a subject I will return to in later chapters.