Object Oriented Programming Unit 5 reflection
More on Classes
Unit 5 notes
Lecturecast notes - Class diagram:
A class diagram is a diagram showing all the classes and the relationships between them.
There are several kinds of relationships between classes:
Objects in one class may contain references to objects in another class. For example, in poker, each Deck contains references to many Cards. Or a tv remote contains references to many types of buttons. This kind of relationship is called HAS-A, as in, “a Deck has a Card”.
A further relationship includes one class that inherits from another class. For example, a Hand inherits from a Deck. This kind of relationship is called IS-A, as in, “a Hand is a kind of a Deck”.
The following class diagram shows the relationships between Card, Deck and Hand:
Codio Inheritance notes
Inheritance is the ability to define a new class that is a modified version of an existing class. Inheritance is a useful feature (and core feature) of object-oriented programming. It can promote the reusability of code since the behaviour of the parent class(es) can be customised (i.e. creating child classes) without having to essentially modify them. On the other hand, inheritance can make programming hard to read. When a method is called, it can be unclear where to find its definition. It can also make debugging difficult because when invoking a method on an object, it may be hard to figure out which method will be invoked.
e-Portfolio activities
Write a Python program with polymorphism that is usable within the summative assessment for the driverless car:
class Driver:
def __init__(self, name):
self.name = name
def set_destination(self):
destination = input("destination set by driver: ")
class Passenger:
def __init__(self, name):
self.name = name
def set_destination(self):
destination = input("destination set by passenger: ")
driver = Driver("Sam")
passenger = Passenger("Patty")
print(driver.name)
print(passenger.name)
for person in (driver, passenger):
person.set_destination()
Will output:
Sam
Patty
destination set by driver: 123 street #inputted by user
destination set by passenger: 123 street #inputted by user
References
Akerblom, B. & Wrigstad, T. (2015) ‘Measuring polymorphism in Python programs’ in: Proceedings of the 11th Symposium on Dynamic Languages (DLS 2015). New York, NY: USA. (pp. 114-128). DOI: 10.1145/2816707.2816717
Lambert, K. A. & Osborne, M. (2010) ‘Software Development, Data Types, and Expressions’ in: Fundamentals of Python: From First Programs through Data Structures. Boston, Massachusetts: USA.