Download presentation
Presentation is loading. Please wait.
Published byApril Heath Modified over 9 years ago
1
Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1
2
Lilian Blot Overview Last term review is-a & has-a Relationship Inheritance Method Overriding Spring 2014 TPOP 2
3
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 2013 TPOP 3
4
Lilian Blot Python and OOP Modules Classes : class keyword Attributes Methods: __repr__(self) for example Constructor: __init__(self,…) Autumn 2013 TPOP 4
5
Lilian Blot Classes in Python Autumn 2013 TPOP 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
6
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 2013 TPOP 6
7
Lilian Blot Cohesion of methods A method should be responsible for one and only one well defined task. Autumn 2013 TPOP
8
Lilian Blot Cohesion of classes Classes should represent one single, well defined entity. Autumn 2013 TPOP
9
Lilian Blot High Cohesion Autumn 2013 TPOP 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
10
Lilian Blot Cohesion of classes It should be represented in the classes hierarchy as well Autumn 2013 TPOP
11
Lilian Blot IS-A RELATIONSHIP Inheritance Spring 2014 TPOP 11
12
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 2014 TPOP 12
13
Lilian Blot Building a Graphic Module list of objects: Square Rectangle Ellipse Circle Triangle Polygon Line Point Canvas Spring 2014 TPOP 13
14
Lilian Blot Building a Graphic Module Spring 2014 TPOP 14 Square Ellipse CircleRectangleTriangle Polygon
15
Lilian Blot Inheritance Hierarchy Spring 2014 TPOP 15 Square Ellipse CircleRectangleTriangle Polygon is-a
16
Lilian Blot Inheritance Hierarchy Spring 2014 TPOP 16 Shape Square Ellipse CircleRectangleTriangle Polygon is-a
17
Lilian Blot Inheritance Hierarchy Spring 2014 TPOP 17 Shape Square Ellipse CircleRectangleTriangle Polygon is-a Parent Class (Superclass) Child Class (Subclass)
18
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 2014 TPOP 18
19
Lilian Blot Inheritance Hierarchy Spring 2014 TPOP 19 Shape Square Ellipse CircleRectangleTriangle Polygon is-a Parent Class (Superclass) Child Class (Subclass) LinePoint GeometricObjectCanvas has-a
20
Lilian Blot Instance Variables and Methods Spring 2014 TPOP 20 Line Point GeometricObject lineColor lineWidth setWidth setColor xyxy getCoord start end getStart getEnd
21
Lilian Blot Implementation see code Spring 2014 TPOP 21
22
Lilian Blot Summary You should have understood the principle of inheritance The notion of method overriding Call to the superclass Constructor Spring 2014 TPOP 22
23
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 subclasses Change 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 2014 TPOP 23
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.