In this blog post, we will write a computer program to create and maintain a multimedia library of books, CDs and DVDs. The aim of this programming task is to demonstrate some key Object-Oriented Programming concepts such as:
- Creating Classes and Objects,
- Using Derived Classes (implementing Inheritance),
- Using an Abstract class,
- Implementing over-riding Polymorphism,
- Implementing over-loading Polymorphism.
This project will be based on the following class diagram:
The Library class will be used to maintain a list of items (Books, DVDs, CDs). It will have a range of methods to:
- Add a new item to the list of items,
- Remove an item from the list of items,
- View the full list of items,
- Reset/Empty the list of items,
- Find out the number if items in the library.
- Find out if the library is empty.
The Item class is an abstract class: It will not be used to instantiate objects directly from this class. Instead, objects will be instantiated from the three derived classes Book, CD and DVD.
This class diagram clearly shows the inheritance relationship between the Item class and the Book, CD and DVD classes:
- The Item class is the parent class (also called master class or super class)
- The Book class, CD class and DVD class are the derived classes (also called sub classes or child classes).
When a class such as the Book class inherit from a parent class (Item class), it inherits all the properties and methods from the parent class. Hence a book will inherit the title and the description properties as well as the viewDescription() method from the Item class.
Additional properties and methods can be added to a derived class. For instance the Book class as its own specific properties: author and isbn.
The viewDescritpion() method is an example of over-riding polymorphism: It has been defined in the parent Item class as an abstract method. It is then implemented differently in the derived classes Book, CD and DVD. You can check the Python code in the trinket below to see the three different implementations of the viewDescription() method in the Book, CD and DVD classes.
We did not implement over-loading polymorphism in this example as it is not directly supported in Python. Over-loading polymorphism is used when the same method is implemented seevral time within a class, each time accepting a different set of parameters (either a different number of parameters or a parameters of different data types).
For instance, we could implement over-loading polymorphism to the addItem() method of the Library class as follows:
def addItem(self, item): #the item parameter is an object (e.g. Book, CD or DVD)#
def addItem(self, title, description, author, isbn):
book = Book(title, desription, author, isbn)
def addItem(self, title, description, artist, genre, numberOfTracks):
cd = CD(title, desription, artist, genre, numberOfTracks)
def addItem(self, title, description, director, certificate):
dvd = DVD(title, desription, director, certificate)
You can read this article to find out how to implement overloading polymorphism in Python using the multipledispatch library.
Here is the Python code for our multimedia library project: