[go: up one dir, main page]

0% found this document useful (0 votes)
48 views15 pages

2: Object Oriented Programming Concepts: What Is An Object?

The document discusses object-oriented programming concepts including objects, classes, encapsulation, inheritance, and polymorphism. It then provides an exercise on using the BlueJ integrated development environment to work with classes and objects representing bicycles. The exercise has the user open a BlueJ project containing Bicycle and Rider classes. They are to create instances of the Bicycle class, inspect their variable values, and call methods to change the variable values. The exercise observes that each object's variables are independent and the speed variable depends on rpm and gear.

Uploaded by

Thomas AJITH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views15 pages

2: Object Oriented Programming Concepts: What Is An Object?

The document discusses object-oriented programming concepts including objects, classes, encapsulation, inheritance, and polymorphism. It then provides an exercise on using the BlueJ integrated development environment to work with classes and objects representing bicycles. The exercise has the user open a BlueJ project containing Bicycle and Rider classes. They are to create instances of the Bicycle class, inspect their variable values, and call methods to change the variable values. The exercise observes that each object's variables are independent and the speed variable depends on rpm and gear.

Uploaded by

Thomas AJITH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Software Development 2 Bell College

2: OBJECT ORIENTED PROGRAMMING CONCEPTS

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.

A software object representing a bicycle could be shown visually like this:

2. Object Oriented Programming Concepts page 1


Software Development 2 Bell College

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.

In practice programmers sometimes choose to expose variables directly to other objects,


or even hide particular methods. Java gives programmers a great deal of control over
encapsulation in their objects.

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.

2. Object Oriented Programming Concepts page 2


Software Development 2 Bell College

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

• The object to which the message is being sent


• The name of the method to perform
• Any parameters needed by the method

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

• An object is a specific instance of a 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.

2. Object Oriented Programming Concepts page 3


Software Development 2 Bell College

EXERCISE: Looking at Classes and Objects


Creating objects

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.

Open the project bicycle1 in BlueJ.

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.

2. Object Oriented Programming Concepts page 4


Software Development 2 Bell College

Object Bench

Right-click on the bicycle1 object and select Inspect from the menu. This allows you to
view the attributes of the object.

List the names and values of the variables shown.

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.

2. Object Oriented Programming Concepts page 5


Software Development 2 Bell College

List the values which have changed.

Create another instance of the Bicycle class. Accept bicycle2 as the name for the new
object. Inspect the variables of bicycle2.

List the values.

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).

Do the variables of the two instances depend on each other?

What does the value of speed depend on?

Class variables and instance variables.

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.

List the name and value of the variable shown.

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.

What was the effect of calling the class method?

Can you change numberOfGears for an individual instance? How do you


know this?

2. Object Oriented Programming Concepts page 6


Software Development 2 Bell College

Looking at Classes and Objects: Object relationships

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.

Inspect the variables of b. What message has been sent to b?

Try out the other methods of rider1 . What messages do they send to b?

2. Object Oriented Programming Concepts page 7


Software Development 2 Bell College

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.

Subclasses can override inherited methods to provide specialised implementations for


those methods. For example, a mountain bike will need a specialised method for
changing gear.

Subclasses provide specialised versions of classes without the need to create


completely new classes. The superclass code can be reused many times over.

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.

2. Object Oriented Programming Concepts page 8


Software Development 2 Bell College

EXERCISE: Looking at inheritance

Open the project bicycle2 in BlueJ.

There is an additional class in this project. MountainBike is a subclass of Bicycle. The


inheritance relationship is shown by the arrow in the project window.

Create a new Bicycle object (bicycle1) and a new MountainBike object (mountain1) in
the same way as before.

Right-click on bicycle1.

List the methods which are available.

Now right-click on mountain1.

List:

• the methods which are available


• the methods which are inherited from Bicycle
• the methods which are inherited from Object

Inspect bicycle1 and mountain1.

List the variables in each, including static ones.

2. Object Oriented Programming Concepts page 9


Software Development 2 Bell College

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.

• How is speed calculated in MountainBike objects?


• Do the class methods in MountainBike affect the Bicycle class? Why?
• What is the result of calling the toString method in each object?

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.

EXERCISE: Looking at polymorphism

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.

2. Object Oriented Programming Concepts page 10


Software Development 2 Bell College

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.

2. Object Oriented Programming Concepts page 11


Software Development 2 Bell College

Call the changeBike method and type bicycle1 in the Method Call dialog box.

Inspect the Bicycle object b belonging to rider1 as before.

What is it’s speed? Why?

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.

What is it’s speed? Why?

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.

This works because MountainBike is a subclass of Bicycle.

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?

2. Object Oriented Programming Concepts page 12


Software Development 2 Bell College

The main types of object relationship

The fundamental task in an object-oriented design is to identify objects in the problem


and then to classify the objects into types. As you divide the problem domain into types,
you will to some degree model the relationships between the types as well. Objects can
have three kinds of relationships:

• the has-a relationship


• the is-a relationship
• the uses-a relationship

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.

• Aggregation: an instance of one class may consist of or include instances of


another class
• Composition: similar to aggregation, except that the included object cannot exist
on its own

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.

2. Object Oriented Programming Concepts page 13


Software Development 2 Bell College

EXERCISE: Looking at object relationships

Open the project bicycle3 in BlueJ.

Use instances of the classes in the project to determine what object


relationships exist in the model, and what type each relationship is. For
example:

relationship type Java implementation


Bicycle has-a Frame composition Bicycle has a variable of type
Frame

2. Object Oriented Programming Concepts page 14


Software Development 2 Bell College

Open the project picture which is in the BlueJ examples folder.

Use instances of the classes in the project to determine what object


relationships exist in the model, and what type each relationship is. For
example:

relationship type Java implementation


Picture has-a Circle aggregation Picture has a variable of type Circle

2. Object Oriented Programming Concepts page 15

You might also like