Upload
duane-craig
View
216
Download
0
Tags:
Embed Size (px)
Citation preview
Object–Orientated Object–Orientated DesignDesign
OOP DesignOOP Design
Describe the following:Describe the following:
a computer a university
Usually the most natural way to describe a task is to list the entities involved in the task.
These entities are referred to as objects.
OOP DesignOOP Design
ObjectsObjects
Objects in general have two important properties:Objects in general have two important properties:
1. State2. Behaviour
OOP DesignOOP Design
Object States
An object contains certain information about itself:e.g.
a lecturer “knows” their name, address, age, courses they teach etc.
a student “knows” their name, address, age, ID, courses studied etc.
a lecture theatre “knows” its location, capacity etc.
OOP DesignOOP Design
The information that an object maintains determines its state.
The individual components of information are known as the objects attributes.
The attributes of an object may be “primitive” values such as integers, characters etc.
e.g. a lecturer’s name, a student’s ID
OOP DesignOOP Design
The attributes of an object may also be other objects.
e.g. a computer may have memory as an attribute but memory may also be an object within the system. The attributes of memory could include capacity, type etc.
Object Behaviour
Apart from maintaining information about itself an object is also capable of performing certain actions.
OOP DesignOOP Design
e.g. a lecturer can teach a class, mark assignments, set an examination papera student can attend a lecture, complete an assignment, sit an exam etc.
The actions that an object can perform are known as its behaviours.
When applying an object-orientated design to a problem specification we identify objects, record their states and specify their behaviour.
OOP DesignOOP Design
Instances and Classes
Generally we can distinguish between two types of object:
1. An instance2. A class
OOP DesignOOP Design
Instances
Instances correspond to individual entities in the application of interest:
e.g. a lecturer called “Mr Jones”a student called “Cathy Smith”
OOP DesignOOP Design
Classes
Classes correspond to classes or templates of an entity:
e.g. lecturer is a class computer is a class student is a class
Each instance in a system must be an instance of some class.
OOP DesignOOP Design
e.g. Mr Jones is an instance of a lecturer Cathy Smith is an instance of a student
Definition of an Object
An object must be an instance of some class.
A class describes the type of information and behaviour that an object can have while an object contains specific information for each attribute in its class.
OOP DesignOOP Design
Super-Classes
Looking closely at our lecturer and student classes it is clear there is some similarity between the states of lecturers and students.
e.g. teachers and students “know” their names, addresses and ages
In object-orientated design we can specify a super-class that contains all the attributes and behaviours that are common between classes.
OOP DesignOOP Design
Consider the super-class person.
The attributes of the person class could be: Name Address Age
The behaviour of the person class could be: Telling name Telling address Telling age
OOP DesignOOP Design
Now the lecturer and student classes are sub-classes of the person super-class.
All sub-classes inherit the properties of their super-classes.
An instance of a sub-class is also automatically an instance of its super-class.
A super-class can also be a sub-class of another super-class etc.
OOP DesignOOP Design
This sub-class super-class relationship can lead to a class or inheritance hierarchy.
Example
Applicance
Machine
Vehicle Computer
Van Car Truck
Mini Delivery Limo Sports Dump Pickup
OOP DesignOOP Design
An object is not only a member of the class it is an instance of; it is also a member of any of its super-classes, as well as a member of any of the super-classes super-classes etc.
This argument also applies to sub-classes; not only is a class a sub-class of its super-class but a sub-class of the super-classes super-classes.
e.g. a pickup is also a truck, a vehicle and a machine.
OOP DesignOOP Design
Each sub-class is said to inherit the state and behaviour of its super-classes i.e. we specify common states and behaviours once in a super-class which any sub-class can then inherit.
OOP DesignOOP Design
Behaviour Overriding
Problem:A shape class contains the length of two dimensions and the behaviour calculate_area as attributes. Three sub-classes of this class are defined as Square, Rectangle and Triangle.
Square
Shape
Rectangle Triangle
OOP DesignOOP Design
The calculate_area behaviour returns the area of the shape by multiplying one dimension by the other.
What problem arises with this class hierarchy ?
The inherited behaviour calculate_area will provide an incorrect answer for an object of class Triangle.
OOP DesignOOP Design
To overcome this problem we can remove the common behaviour from the class shape and incorporate separate behaviours in all three sub-classes Square, Rectangle and Triangle.
Each sub-class class can contain the same behaviour named calculate_area. This is known as polymorphism.
This method is perfectly legal but there is some duplication in behaviour particularly between the Rectangle and Square classes.
OOP DesignOOP Design
Fortunately Object-Orientated Design provides a more efficient and natural way to overcome this problem.
A subclass can override the behaviour defined in a super-class i.e. if a specific behaviour is defined in a class as well as its super-class then the class behaviour takes priority.
e.g. the generic behaviour calculate_area can be inherited by the Square and Rectangle classes but we can override the calculate_area behaviour in the Triangle class.