In Sectionย 3.2ย APIs and Libraries we saw how classes define new data types whose values share a set of attributes and behaviors. We also saw how different objects, while having the same abstract attributes, each have their own values for those attributes and how an objectโs values can then affect their behavior in some way. For instance all Turtle objects have a position but two Turtle objects can be at different positions on the screen. And both can be made to move forward from wherever they are with the forward() method.
To review the vocabulary, a class defines the attributes and behaviors that all objects of that class will have. From a class we make many objects also called instances of the class. Each instance has its own values for the attributes defined in the class. Attributes are the data or properties that an object knows about itself, for example, a turtle objectโs color and size. Behaviors are the things that an object can do. For example, a turtle object can go forward, turn left, or turn right. The attributes and behaviors of a class are defined in the class, but each object has its own values for the attributes.
The following picture has lots of cats (objects of the type cat). They are all different, but they share the same attributes and behaviors that make up a cat. They are all instances of cat with different values for their attributes. Name some of the attributes and behaviors of the cats below. For example, the color ( attribute) of the first cat is black ( attribute value) and it is playing ( behavior).
But thinking in terms of abstract attributes and behaviors is the outside view of a class, for when weโre using an API. Now itโs time to open up the black box and see how classes are structured on the inside. In this section weโll take a very quick tour through the main parts of a class. Then weโll go through each part in more detail in the following sections.
Subsection8.1.1Quick tour: instance variables, constructors, and methods
When we open up the black box, we find classes have three main parts: instance variables, constructors, and methods. In order to have something to talk about, letโs look at a simple but complete class, Person.
The purpose of this class is to model a person as someone who has two attributes, a name and an age. A person also has two behaviors: they can print out a greeting where they introduce themselves and they can answer the boolean question of whether they can vote, which depends on whether they are eighteen or older.
Person person1 = new Person("Joe", 18);
Person person2 = new person("Sally", 17);
person.greet(person2);
System.out.println("Joe can vote: " + person1.canVote());
System.out.println("Sally can vote: " + person2.canVote());
Almost all the classes we write will start with the keyword public. It just means the class can be used in other classes. We will occasionally have to write classes without the public in exercises in this book but only because of limitations of the platform this book runs on. However, the really important word in this line is class which says weโre defining a class.
Next is the name of the class, Person. Class names in Java are always capitalized. It is not strictly required by the language but is as universally followed a convention as just about anything in programming. Consequenly, Java code with uncapitalized class names can be extremely confusing, especially to experienced Java programmers, so donโt write code with uncapitalized class names!
After the name of the class comes the opening { of the class body which, like the body of a method, is defined inside a pair of {}s. Everything that makes up the definition of the class must be inside that pair of {}.
The only new thing here, compared to other variable declarations weโve seen, is the keyword private. After that, they look like regular variable declarations with a type and a name. But because these variables are defined at the top level of the class, meaning not inside a method, they are called instance variables and they are what define the attributes instances of this class have. So from these two lines we can see that every Person object has two attributes, a name and an age.
These variables define the structure of a Person object and they are called instance variables because each instance of the class, that is each object, has its own set of variables that arenโt shared with other instances.
The next thing we define in a class are usually its constructors. Weโll talk a lot more about constructors in a future section but the short version is, a constructorโs job is to initialize an objectโs instance variables. Our Person class has one constructor:
public Person(String name, int age) {
this.name = name;
this.age = age;
}
Weโll get into the details soon but for now, just know that this code runs we write something like new Person("Joe", 18) and the arguments "Joe" and 18 become the values of the parameters name and age. Those values are then assigned to this.name and this.age which are specifically the instance variables we defined above. This constructor guarantees that whenever we make a Person we have to supply a name and an age and those values are stored in the objectโs intstance variables.
Finally the behaviors of this class, emitting a greeting, and answering the question of whether a specific Person can vote, are provided by the two methods:
public void greet(Person other) {
System.out.println("Hello " + other.name + ", I'm " + name + ".");
}
public boolean canVote() {
return age >= 18;
}
You should be familiar with how methods work from Sectionย 3.1ย Writing methods but there are two new things here. First, both of these methods refer to a variable that is neither a parameter nor is declared inside the method. The method greet has one parameter, to, but it also refers to the variable name. And canVote has no parameters but refers to the variable age.
This works because of the second new thing that is that these methods do not have the keyword static in their signature. That makes them instance methods which means they have access to the instance variables defined in the class.
Watch this short video to review some of the key vocabulary of object-oriented programming and then weโll move on to a more detailed examination of these three parts of a class.