2: Object Oriented Programming Concepts: What Is An Object?
2: Object Oriented Programming Concepts: What Is An Object?
What is an Object?.......................................................................................................... 1
Encapsulation.................................................................................................................. 2
Information hiding........................................................................................................... 2
Messages ......................................................................................................................... 3
What is a Class?.............................................................................................................. 3
What is BlueJ? ................................................................................................................ 3
EXERCISE: Looking at Classes and Objects ............................................................... 4
Inheritance ....................................................................................................................... 8
EXERCISE: Looking at inheritance ............................................................................... 9
Polymorphism ............................................................................................................... 10
EXERCISE: Looking at polymorphism........................................................................ 10
The main types of object relationship......................................................................... 13
EXERCISE: Looking at object relationships............................................................... 14
What is an Object?
If you write an object-oriented computer program you are creating, in your computer, a
model of some part of the world. The parts that the model is built up from are the objects
that appear in the real world. These objects are represented in the computer model
being created.
You can look around you and see many examples of real world objects: a dog, a car, a
cash machine, a bicycle.
These real-world objects all have state and behaviour. For example:
State Behaviour
Dog Name Bark
Colour Wag tail
Breed Fetch
Happy Eat
Bicycle Gear Speeding up
Pedal rpm Slowing down
Number of gears Changing gears
Two wheels
Software objects can be used to represent real-world objects. Like real-world objects,
software objects have state and behaviour. A software object has variables (often also
called attributes) to maintain its state, and methods to implement its behaviour.
Bicycle
numberOfGears
rpm
variables
speed
currentGear
changeGear
methods
changeRpm
In addition to its variables, the bicycle object has methods to change pedal rpm and
change gear. It does not need a method to change speed, as the speed depends
entirely on the pedal rpm and the current gear.
A single object is not particularly useful on its own. An object oriented program consists
of a number of software objects which can interact with each other, just as real-world
objects can interact.
Encapsulation
In the bicycle object, the values of variables are changed by the methods. The
changeGear method changes the value of the currentGear variable. The speed variable
is changed by either the changeGear or changeRpm methods.
The methods are hiding the objects variables from other objects in the program.
Packaging variables within the protection of methods is called encapsulation.
Information hiding
An object has a public interface which other objects use to communicate with it. Because
of encapsulation, the details of how state is stored or how behaviour is implemented can
be hidden from other objects. This means that the object’s private details can be
changed without affecting other objects which depend on it. For example, a rider does
not need to know exactly how the mechanism which changes gear on the bicycle
actually works in order to use it.
Hiding design or implementation details from other objects is called information hiding.
Messages
Software objects interact and communicate with each other by sending messages. For
example, if a rider object wants to make a bicycle object perform its changeGear
method, then the rider needs to send a message to the bicycle object.
Sometimes the receiving object needs more information. For example, the rider needs to
specify what gear the bicycle is to change to. This information is a parameter of the
message.
A message consists of
What is a Class?
In the real world there are often many objects of the same kind. A single bicycle is just
one of many bicycles in the world. Similarly, an object oriented program might have
many objects of the same kind which share common characteristics.
A class is a blueprint for objects of the same type. A Bicycle class is a blueprint from
which many bicycle objects are created. Each bicycle object is an instance of the Bicycle
class, and its state can be different from that of other bicycles. One bicycle might
currently be in 5th gear, while another might be in 3rd gear.
• A class is a blueprint which defines the variables and methods common to all
objects of that class
Until an instance of the class is created, no actual objects exist to be used in a program.
What is BlueJ?
BlueJ is a Java integrated development environment (IDE) which has been designed
specifically for learning object oriented programming in Java. It is more convenient to
use than the standard Java command line tools, and easier to learn than a full-featured
IDE such as NetBeans or Eclipse. It will also help you understand how the classes in
your object oriented programs are related to each other.
You can now look at some software classes and objects created in Java. BlueJ allows us
to create classes and objects and test their interaction without actually creating a
program which ‘runs’.
Download the file bicycles.exe from your course web site, and extract the contents. You
should have several folders containing BlueJ projects.
The project contains two classes, Bicycle and Rider. If these are shown as cross-
hatched rectangles, click the Compile button. Double-click on the Bicycle class to see
the code.
We will not examine the code in detail yet, but you should try to identify the variables and
methods.
There are no objects yet. You should create a bicycle object (i.e. an instance of the
Bicycle class). Right-click on the Bicycle class, and select new Bicycle() from the pop-up
menu. Accept bicycle1 as the name of the object to be created. The new object is shown
in the Object Bench at the bottom of the project window.
Object Bench
Right-click on the bicycle1 object and select Inspect from the menu. This allows you to
view the attributes of the object.
You will now send a message to bicycle1 to tell it to change rpm. In BlueJ you can send
a message directly to an object. Right-click on the bicycle1 object. The two available
methods are listed in the menu.
Select void changeRpm(int). Enter a value of 50 for the parameter in the Method Call
dialog box which appears. Inspect the object’s variables again.
Create another instance of the Bicycle class. Accept bicycle2 as the name for the new
object. Inspect the variables of bicycle2.
Call the changeGear and changeRpm methods of both instances some more times (note
that changeGear will change up for a parameter of 1, down for 0 and no change
otherwise).
A class variable is a variable which is common to all instances of the class. The
variable numberOfGears in the Bicycle class is a class variable – all Bicycle objects will
have the same number of gears.
Class variables are also known as static fields. When you see the word static in Java
you can assume that it means that something belongs to a class rather than to a specific
instance of the class.
Inspect the variables of bicycle1. Click the Show static fields button.
The value of the class variable is changed by a class method. Right click on the Bicycle
class (the class itself, not an instance) – note that there is a method called void
changeNumberOfGears(int) listed in the menu. Select this and enter 18 for the
parameter.
Now inspect the variables of the two instances (remember to look at the static fields).
List the values.
In the project window, the Rider class is connected by a dotted arrow to the Bicycle
class. This arrow represents a Uses relationship. This means that a rider object can
send a message to a bicycle object.
Right-click on the Rider class and select new Rider() from the menu. Accept rider1 as a
name for the new instance, which is then shown alongside the previously created Bicycle
objects. Inspect the variables for this object.
Note that one of the variables, called b, is of type Bicycle – this means that it is a new
instance of the Bicycle class. This is a different instance from the ones you created
before. It does not appear directly in the project window as it only exists through its
relationship to the rider object.
You can inspect the variables of this new Bicycle object b by clicking on it in the rider_1
object inspector window and clicking the Inspect button. This opens a new object
inspector showing the variables of b.
The Rider object can send messages to its Bicycle object. Right-click on the rider1 object
and select void speedup() from the menu.
Try out the other methods of rider1 . What messages do they send to b?
Inheritance
Object oriented systems allow classes to be defined in terms of other classes. For
example, mountain bikes and racing bikes are kinds of bicycles. We can define a
MountainBike class and a RacingBike class which are subclasses of the Bicycle class.
The Bicycle class is then the superclass of MountainBike and RacingBike.
Subclasses inherit state and behaviour from their superclass. For example, a mountain
bike has gear, rpm and speed, like any bicycle, but also has several ranges of gears – it
needs an extra variable gearRange.
Class hierarchy
You are not limited to just one class of inheritance. The inheritance tree, or class
hierarchy, can be as deep as needed.
In Java, there is a class called the Object class at the top of the hierarchy. All classes
are descended from Object, and inherit behaviour from it.
Create a new Bicycle object (bicycle1) and a new MountainBike object (mountain1) in
the same way as before.
Right-click on bicycle1.
List:
Note that mountain1 has all the variables that bicycle1 has, with two additional variables.
Try using the methods of mountain1 and the class methods of the MountainBike class
and answer the following.
Polymorphism
The word polymorphism literally means multiple forms. For example, a rider may own
several bicycles of different types. The way in which each type of bicycle implements
gear changing may be different, but the message which the rider sends to make the bike
change gear will be the same in each case.
The same message is used, but multiple forms of the changeGear method can be
invoked. The ability to send the same message to several different types of receivers
and have the message trigger the right method greatly simplifies message sending.
Open the project bicycle2 in BlueJ. Create Bicycle (bicycle1) and MountainBike
(mountain1) objects if these are not already created. Create a new Rider object (rider1).
Call methods of bicycle1 as required to set its speed to 50. Call methods of mountain1
as required to set its speed to 100.
Inspect the Bicycle object b belonging to rider_1 as before. Note that its speed is 0.
Right-click on rider1 to see the methods available. A Rider object has a method
changeBike. The parameter for this method is of type Bicycle. This method is called by
supplying the name of a specific Bicycle object.
Call the changeBike method and type bicycle1 in the Method Call dialog box.
Now, call the changeBike method of rider1 and type mountain1 in the Method Call dialog
box. Inspect the Bicycle object b belonging to rider1 as before.
Note that the Rider’s Bicycle object is now actually a MountainBike object.
This shows polymorphism in action – the Rider sends a message to a Bicycle object, but
it can also send the same message to a MountainBike object.
Question: the rider only knows how to ride a bicycle. When the rider gets
on a mountain bike, he does not know how to use its specialised features
(i.e. the extra gear ranges). How can we allow a rider in our model to use
the full features of a mountain bike?
The has-a relationship means that one type of object contains another or is composed of
another. Some examples are: a car has-an engine, a bicycle has-a wheel, and a coffee
cup has coffee. The has-a relationship is modeled with aggregation or composition.
In Java the included instance is a variable in the class. For example, a Car class might
have a variable of type Engine.
The is-a relationship means that one type of object is a more specific version of a
general type. Some examples are: a car is-a vehicle, a bicycle is-a vehicle, and a coffee
cup is-a cup. The is-a relationship is modeled with inheritance. For example, a Car class
might be a subclass of a Vehicle class.
The uses-a relationship means that during some activity, one type of object uses another
type of object. Some examples are: a car uses-a squeegee (during a window-washing
activity), a bicycle uses-a pump (during a tyre-pumping activity), and a coffee cup uses-a
stirrer (during a stirring activity). In Java this means that the using class sends messages
to an instance of the used class. For example, a Car class might have a method called
wash with a parameter of type Squeegee.