Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

23
Lilian Blot Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1

Transcript of Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Page 1: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian BlotLilian Blot

INHERITANCE

Object Oriented Programming

Spring 2014TPOP

1

Page 2: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

Overview

Last term review

is-a & has-a Relationship

Inheritance

Method Overriding

Spring 2014TPOP

2

Page 3: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

What are Classes?

Classes are composed from structural and behavioural constituents.

Data field members (member variables or instance variables) enable a class instance to maintain state.  a.k.a properties, fields, data members, or attributes

Methods, enable the behaviour of class instances.

Classes define the type of their instances

Autumn 2013TPOP

3

Page 4: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

Python and OOP

ModulesClasses : class keywordAttributesMethods: __repr__(self) for exampleConstructor: __init__(self,…)

Autumn 2013TPOP

4

Page 5: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

Classes in Python

Autumn 2013TPOP

5

class QueueOOP: def __init__(self): self._head = None ## Pointer to front of queue, _Node object self._tail = None ## Pointer to back of the queue , _Node object self._size = 0

def dequeue(self):…

def enqueue(self,element):…

Class definition

def dequeue(self): if self.isempty(): raise IndexError('dequeue from empty queue') else:

element = self._head.datum self._head = self._head.next if self._head is None: self._tail = None self._size -= 1 return element

Method definition

Page 6: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

Encapsulation as Information hiding

Data belonging to one object is hidden from other objects.

Know what an object can do, not how it does it.

Information hiding increases the level of independence.

Independence of modules is important for large systems and maintenance.

Autumn 2013TPOP

6

Page 7: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

Cohesion of methods

A method should be responsible for one and only one well defined task.

Autumn 2013TPOP

Page 8: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

Cohesion of classes

Classes should represent one single, well defined entity.

Autumn 2013TPOP

Page 9: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

High Cohesion

Autumn 2013TPOP

9

The class QueueOOP contains only method necessary for this type of ADT. does NOT implement add_first, insert_at, remove, etc.

If the user need such methods, he/she must use a different ADT (not a queue).

Several ADTs could/should be implemented in the same module

Page 10: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

Cohesion of classes

It should be represented in the classes hierarchy as well

Autumn 2013TPOP

Page 11: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian BlotLilian Blot

IS -A RELATIONSHIP

Inheritance

Spring 2014TPOP

11

Page 12: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

is-a & has-a Relationship

The is-a relationship describes two objects where one object is more specific instance of the other

The has-a relationship describes two object where one object use another object

Example: A Rectangle is-a more specific instance of a Polygon A Circle has-a centre Point

Spring 2014TPOP

12

Page 13: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

Building a Graphic Module

list of objects: Square Rectangle Ellipse Circle Triangle Polygon Line Point Canvas

Spring 2014TPOP

13

Page 14: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

Building a Graphic Module

Spring 2014TPOP

14

Square

Ellipse

CircleRectangl

eTriangle

Polygon

Page 15: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

Inheritance Hierarchy

Spring 2014TPOP

15

Square

Ellipse

CircleRectangl

eTriangle

Polygon

is-a is-ais-a

is-a

Page 16: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

Inheritance Hierarchy

Spring 2014TPOP

16

Shape

Square

Ellipse

CircleRectangl

eTriangle

Polygon

is-a is-ais-a

is-a

is-a is-a

Page 17: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

Inheritance Hierarchy

Spring 2014TPOP

17

Shape

Square

Ellipse

CircleRectangl

eTriangle

Polygon

is-a is-ais-a

is-a

is-a is-a

Parent Class (Superclass)

Child Class (Subclass)

Page 18: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

The Next Step in Design

What things should each object know? e.g. Instance variables

fill colour Outline colour Position in canvas Line width

What things should each object be able to do? e.g. Methods

Change the fill colour: setFill(colour) ….

What are the commonalities between objects?

Spring 2014TPOP

18

Page 19: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

Inheritance Hierarchy

Spring 2014TPOP

19

Shape

Square

Ellipse

CircleRectangl

eTriangle

Polygon

is-a is-ais-a

is-a

is-a is-a

Parent Class (Superclass)

Child Class (Subclass)

LinePoint

GeometricObject

Canvashas-a

Page 20: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

Instance Variables and Methods

Spring 2014TPOP

20

LinePoint

GeometricObject

lineColorlineWidth

setWidthsetColor

xy

getCoord

startend

getStartgetEnd

Page 21: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

Implementation

see code

Spring 2014TPOP

21

Page 22: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

Summary

You should have understood the principle of inheritance

The notion of method overriding

Call to the superclass Constructor

Spring 2014TPOP

22

Page 23: Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.

Lilian Blot

Exercises

Write the remaining subclasses of shape. What other shape can you devise?What other attribute could be added to shape

or its subclassesChange the draw method, such as given a

turtle graphic object in its parameter, it draw the shape on the canvas. You can use the code from the Battleship program to learn how to draw using turtle.

Spring 2014TPOP

23