One Book model, several kinds of books.
Inheritance keeps shared title, author, year, and borrowing behavior in Book. A child class such as EBook(Book) receives that behavior.
class Book:
def __init__(self, title, author, year):
self.title = title
self.author = author
self.year = year
self.is_available = True
def borrow(self):
self.is_available = False
def display_info(self):
print(f"{self.title} - {self.author}")
class EBook(Book):
pass
ebook = EBook("Dune", "Frank Herbert", 1965)
ebook.display_info()
ebook.borrow()
print(ebook.is_available)EBook inherits the constructor, display_info(), and borrow() even though it defines none of them. This is a simplified earlier Book example; the full project still validates year.
Dune - Frank Herbert
Falseborrow() receives the EBook object as self, so it changes only that object’s is_available value.
Parent and child: Book is the parent class. EBook is a child class. The syntax class EBook(Book): creates the inheritance link.
class EBook(Book):
def open_preview(self):
print(f"Opening preview: {self.title}")
class PrintedBook(Book):
def show_shelf_info(self):
print(f"Find '{self.title}' on the library shelf.")Both child classes keep inherited Book methods, while each adds its own new method.
