You are on page 1of 28

Object oriented

OCR programming
A Level Unit 11
Computer Science Programming techniques
H446 – Paper 2

6
Objectives
• Describe the features of an object oriented language:
• classes, objects, methods, attributes, inheritance,
encapsulation and polymorphism

• Write pseudocode for a class definition


• Write pseudocode to instantiate an object and use its
methods
• Draw inheritance diagrams
• Describe the advantages of an object oriented
approach to programming
Object-oriented programming
Unit 11 Programming techniques

Object oriented programming


• By now you should have some experience in using
procedural programming to solve problems
• Object oriented programming (OOP) uses many of
the same structures
• Many programming languages such as Python and
VB.NET support both procedural programming and
OOP
• Syntax will vary according to the language, so we
will use a pseudocode version of OOP to show
general principles
Object-oriented programming
Unit 11 Programming techniques

Classes
• In OOP, a class is a blueprint for an object
• It defines what an object in the class will look like (its
properties), and what it can do (its methods)
• A class may implement an abstract data type such
as an array or fraction, or a more concrete object
such as a person or car
Object-oriented programming
Unit 11 Programming techniques

Attributes and methods


• A class will define the attributes (properties) of
objects in the class
• It will also define the methods (procedures) that an
object in the class can perform
• Suggest some attributes and methods for a class
named Dog
Object-oriented programming
Unit 11 Programming techniques

Attributes and methods for Dog


• The attributes given to the class Dog will depend on
what the objects in the class are going to do
• A dog could have attributes name, colour, peopleRescued
• Methods could include, search, dig, bark, rescue

• The programmer will also need methods to set (or


change) and get the attributes
Object-oriented programming
Unit 11 Programming techniques

Defining a class in pseudocode


class Dog
private name
private colour
public procedure new (myName, myColour)
name = myName
colour = myColour
endprocedure

public procedure bark(barkTimes)


for n = 1 to barktimes
print (“Woof!”)
next n
endprocedure
endclass
Object-oriented programming
Unit 11 Programming techniques

Public and private


• Refer to Worksheet 1, question 1 for the definition
of the Dog class
• Attributes are normally declared as private
• Methods (procedures) are generally declared as
public
• This is to ensure that attributes cannot be changed
except by executing a method
• Notice the procedure new – this procedure is a constructor
• It is used to create new Dog objects
Object-oriented programming
Unit 11 Programming techniques

Encapsulation and data hiding


• Encapsulation wraps the attributes and methods in
the class definition, hiding details of the
implementation from the user
• For example, in Python to sort a list of names called
names, you can write
• names.sort ()

• You have no idea how the list of names is sorted or


what attributes are defined in the class List, of which
names is an object
• You simply need to know the name of the method and any
parameters needed
Object-oriented programming
Unit 11 Programming techniques

Creating a new object


• So far, we have created a blueprint for a dog, but we
have not created, or instantiated, any dogs
• To create two new dogs called Fido and Bonnie, we
can write
• myDog1 = new Dog(“Fido”, “black”)
• myDog2 = new Dog(“Bonnie”, “golden”)

• To make Fido bark 3 times, we write


• myDog1.bark(3)
Object-oriented programming
Unit 11 Programming techniques

Setter and getter methods


• We often need a means to set an attribute, or to find
out the value of an attribute
• Suppose Fido’s colour was wrongly recorded as
“Black” and we wish to change it to “Brown”
• An important characteristic of OOP is data hiding
• This means that a programmer who defines an
object in a class cannot directly access or change an
attribute of the object
• It has to be done via a “getter” or “setter” method
Object-oriented programming
Unit 11 Programming techniques

A setter method
• In the definition of class Dog, we can add two
more procedures:
public procedure setColour(dogColour)
colour = dogColour
endprocedure

public procedure getColour()


return colour
endprocedure
Object-oriented programming
Unit 11 Programming techniques

Worksheet 6
• Try the questions in Task 1
Object-oriented programming
Unit 11 Programming techniques

Summary so far
• What is a class?

• What is an object?

• What is a constructor?

• What is instantiation?
Object-oriented programming
Unit 11 Programming techniques

Summary so far
• What is a class?
• A template for an object which defines its attributes
and methods
• What is an object?
• An instance of a class

• What is a constructor?
• A method used to create a new object in a class

• What is instantiation?
• Creating an instance of an object
Object-oriented programming
Unit 11 Programming techniques

Inheritance
• An important feature of OOP is the ability to define
“child” classes which inherit the attributes and
methods of a “parent” class
• We could define a Pet class which has attributes
type, name and colour and methods eat,
makeNoise, move
• “Child” classes could be Cat, Dog and we would not need to
redefine all the methods and attributes that are in the Animal
class
• We can add extra attributes and methods to each child class,
and redefine methods is need be
Object-oriented programming
Unit 11 Programming techniques

Inheritance diagram
• Inheritance can be shown in a diagram:
Object-oriented programming
Unit 11 Programming techniques

Inheritance example
• We have defined a class Dog which has attributes
name and colour, a constructor called new, and
methods including one called bark
• Suppose we want to create a new class
called Puppy
• This will have all the
attributes and methods of
Dog, but will have an
additional attribute
shoesChewed and an
additional method
chewShoe
Object-oriented programming
Unit 11 Programming techniques

Inheritance example
• Using OOP there is no need to redefine all the
attributes and methods defined in the Dog class
• We can specify that the Puppy class will inherit
these from the superclass Dog
• In addition, the Puppy class needs an extra attribute
shoesChewed, initialised to 0
Class Puppy inherits Dog
private shoesChewed
shoesChewed = 0
Object-oriented programming
Unit 11 Programming techniques

Adding further methods


• We need to keep count of the number of shoes that
the puppy chews
• What other methods do we need to add to the Puppy class?
Object-oriented programming
Unit 11 Programming techniques

Adding further methods


• We need a method chewShoe(numShoes) or
something similar
• This will add numShoes to the total of ShoesChewed
• We also need a method getShoesChewed
Object-oriented programming
Unit 11 Programming techniques

Worksheet 6
• Ask students to do the questions in Task 2
Object-oriented programming
Unit 11 Programming techniques

A Puppy constructor
• Suppose the Puppy class also has an extra attribute,
date of birth (dob), which will be set when a Puppy
object is created
Class Puppy inherits Dog
private shoesChewed = 0
public procedure new (myName, myColour,
myDob)
super.new(myName, myColour)
dob = myDob
• super.new indicates that the name and colour
attributes are inherited from the superclass
Object-oriented programming
Unit 11 Programming techniques

Polymorphism
• An inherited class may redefine methods that are
defined the parent class
• For example, we can redefine the bark method in the
Puppy class so that it prints out “Yap” instead of
“Woof”
• The ability of a programming language to process
objects differently depending on their class is called
polymorphism
Object-oriented programming
Unit 11 Programming techniques

Worksheet 6
• Try Tasks 3 and 4 on the worksheet
Object-oriented programming
Unit 11 Programming techniques

Advantages of OOP
• Code reuse
• Classes created in one program can easily be saved in a
library and reused in other programs – this leads to faster
program development
• Encapsulation reduces code complexity
• Once an object is created, it is not necessary to know how its
methods are implemented
• Attributes can be hidden from users so that they cannot
change them accidentally
• Software maintenance
• Object oriented programs are easier to modify and maintain
Object-oriented programming
Unit 11 Programming techniques

Plenary
• Can you describe the features of an object oriented
language?
• classes, objects, methods, attributes, inheritance,
encapsulation and polymorphism
• You have written class definitions and algorithms in
pseudocode, and hopefully written some object-
oriented programs as well
• You should be able to describe the advantages of an
object oriented approach to programming
Object-oriented programming
Unit 11 Programming techniques

Copyright

© 2016 PG Online Limited

The contents of this unit are protected by copyright.

This unit and all the worksheets, PowerPoint presentations, teaching guides and other associated files
distributed with it are supplied to you by PG Online Limited under licence and may be used and copied by you
only in accordance with the terms of the licence. Except as expressly permitted by the licence, no part of the
materials distributed with this unit may be used, reproduced, stored in a retrieval system, or transmitted, in any
form or by any means, electronic or otherwise, without the prior written permission of PG Online Limited.

Licence agreement

This is a legal agreement between you, the end user, and PG Online Limited. This unit and all the worksheets,
PowerPoint presentations, teaching guides and other associated files distributed with it is licensed, not sold, to
you by PG Online Limited for use under the terms of the licence.

The materials distributed with this unit may be freely copied and used by members of a single institution on a
single site only. You are not permitted to share in any way any of the materials or part of the materials with any
third party, including users on another site or individuals who are members of a separate institution. You
acknowledge that the materials must remain with you, the licencing institution, and no part of the materials may
be transferred to another institution. You also agree not to procure, authorise, encourage, facilitate or enable any
third party to reproduce these materials in whole or in part without the prior permission of PG Online Limited.

You might also like