Professional Documents
Culture Documents
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
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
A setter method
• In the definition of class Dog, we can add two
more procedures:
public procedure setColour(dogColour)
colour = dogColour
endprocedure
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
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
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.