Master Python Object-Oriented Programming with this hands-on lab exercise. Build a Library Management System to learn encapsulation, inheritance, and polymorphism through practical coding examples.
This lab practical is designed to reinforce the three pillars of Object-Oriented Programming (OOP) by building a Library Management System.
Encapsulation involves bundling data (attributes) and methods into a single unit and restricting direct access to some components using “private” members (indicated by a double underscore __).
Book class.__title and __author so they cannot be changed directly from outside the class.class Book:
def __init__(self, title, author):
self.__title = title # Private attribute
self.__author = author
def get_details(self):
return f"'{self.__title}' by {self.__author}"
# Getter for title
def get_title(self):
return self.__title
Inheritance allows a class (child) to derive attributes and methods from another class (parent).
DigitalBook class that inherits from Book.super().__init__() to initialize attributes from the parent class.file_size.class DigitalBook(Book):
def __init__(self, title, author, file_size):
super().__init__(title, author)
self.file_size = file_size
def get_details(self):
# Extending the parent method
return f"{super().get_details()} [Size: {self.file_size}MB]"
Polymorphism allows different classes to be treated as instances of the same general class through the same interface (method name), even if their underlying logic differs.
PaperBook class.get_details() method name.class PaperBook(Book):
def __init__(self, title, author, weight):
super().__init__(title, author)
self.weight = weight
def get_details(self):
return f"{super().get_details()} [Weight: {self.weight}g]"
# Polymorphism in action
library = [
DigitalBook("Python Crash Course", "Eric Matthes", 5),
PaperBook("Automate the Boring Stuff", "Al Sweigart", 500)
]
for book in library:
print(book.get_details())
| Concept | Implementation in this Lab |
|---|---|
| Encapsulation | Hiding __title and using get_title() to control access. |
| Inheritance | DigitalBook and PaperBook reusing code from the Book class. |
| Polymorphism | Calling get_details() on any book without needing to know its specific type. |
Book class to include a private attribute __is_checked_out (boolean).toggle_checkout() that flips this boolean.get_details() method in a new subclass called ReferenceBook which always returns “Cannot be checked out” regardless of the status.