A Bit About Objects
As mentioned in Lesson 1: Introduction, Java is an object-oriented language. Programs are constructed using one or more objects which communicate to accomplish some task. It is time to learn something about these things called objects.
Note: This lesson and the next are fundamental to understanding object-oriented programming. Read them carefully and make sure you understand the concepts.
What is an Object?
Objects used in programming are a bit like objects in the real world. Physical objects have two characteristics: the state they are in (i.e., their properties or attributes) and the behaviors they can perform.
Consider a CD player. Its state may consist of things like:
- Title of CD currently loaded
- Track number
- Volume setting
Likewise, its behaviors may include:
- Load a CD
- Skip forward
- Skip backward
- Change volume
Pictorially this could be represented as follows:
Software objects are similar to real world objects in that they also have state and behaviors. The state of an object is stored in variables and their behaviors are implemented with methods. Variables are like named cubbyholes in which you can store data (i.e., values) for later use. Methods are named pieces of code that implement behaviors the object is capable of.
Your computer probably has a CD player application on it. This application might have a software object representing a CD player with variables and methods corresponding to the states and behaviors listed above. Such an object can be represented as shown below. A variable called "title" holds the character string "Synchronicity." Likewise there are variables "trackNumber," "volume" and "playingStatus" for holding the other pieces of state information.
Of course, software objects don't always have physical world counterparts. It is common to model more abstract concepts using objects. The "turtle" objects you will use in Jurtle are an example of this.
While many of your initial Jurtle programs will just use a single object, in larger programs this is rare. Generally many different objects are used together to accomplish the program's task. This is again like the real world. Your CD player is composed of many parts, each of which is an object in its own right.
What is a Class?
In the real world there may be many objects of the same basic kind. For example, there are many CD players like yours, all made by the same manufacturer. In object-oriented terminology, your personal CD player is an instance of the generic class of CD players of that make. Each instance has its own state (e.g., each may have a different CD loaded) but they all have the same methods defining their behavior.
When a manufacturer builds a CD player, it uses a blueprint to know how to construct it. All CD players of that same model can be built using the same blueprint. Likewise, in object-oriented programming you need a blueprint for constructing object instances. These software blueprints are called classes and are written by you, the programmer.
For example, when writing a CD player class, you would declare instance variables to hold the player's state (title of CD loaded, current track, etc.). You would also declare and write instructions for the methods that the CD player implements (play, skip, etc).
After writing the class definition, you can use it to create objects of that class by instantiating them. When created, each object has memory allocated to it to hold its instance variables (i.e., its state). The state of each object is separate from that of the others. After an object is created, you can call its instance methods to have it do something.
In addition to instance variables and instance methods, the class can define static variables and static methods. Static variables are not duplicated in each instance. They store properties that belongs to the class as a whole. Each instance of the class will get the same value when accessing a static variable. Collectively, instance variables and static variables are referred to as member variables because they are members of the class.
Static methods are similar to instance methods except the class, rather than an instance of that class, executes the method. Because the class itself is executing the static method, it cannot use any instance variables, only static variables. This is because there is no instance to get the variable values from.
What is Inheritance?
In the real world, everything can be placed in a kind of hierarchy or classification scheme. For example:
So a Cat is a kind of a Mammal, which is a kind of Animal, which is a kind of Thing.
In object-oriented programming, classes also fit into a hierarchy. It is referred to as a class inheritance hierarchy because classes underneath inherit designated variables and methods from the classes above them. Classes above a given class are called superclasses of that class. Classes below a class are called subclasses. When you create a new class you always specify its superclass to position it in the hierarchy.
Consider the following Java object inheritance hierarchy that might be used in an Artificial Life simulation:
Object is a special class in Java that all other classes ultimately inherit from -- it is at the top (also called the root) of the inheritance hierarchy. Object doesn't have any instance variables but it does have a small number of methods.
SimAnimal comes next as a subclass of Object. It inherits any methods that Object has. This means that if you call a method implemented in Object on a SimAnimal instance, it will know how to perform it.
SimMammal inherits from SimAnimal. This means it shares any instance variables (i.e. state) that SimAnimal defines, plus it is able to perform methods defined in either SimMammal, SimAnimal, or Object.
Finally SimCat comes at the bottom of the hierarchy. It shares all the instance variables of those classes above it (its superclasses). It is also able to perform any methods that either it implements or its superclasses implement.
Inheritance is useful in object-oriented programming because a class does not have to re-implement behaviors or properties found in its superclasses. For example, suppose the class SimMammal had an instance variable called isNursing and a method called nurse. Then SimCat could just inherit those since cats also nurse their young.
In many cases a subclass will need to implement a behavior found in its superclass, but in a slightly different way. In this case the subclass can override the method found in the superclass. It does so by implementing a method with the same name and same parameters as the superclass's method, but with a different behavior.
Think of an object in the real world that you might model with a Java class. Create a list of its instance variables. Do the same for the object's methods.
In Java, variable and method names generally start with a lowercase letter and shouldn't contain spaces or punctuation characters. The name should be descriptive. If it is composed of more than one word they should be run together with the first letter of all but the first word capitalized (e.g., numberOfSprockets).