✦ Python Intermediate & OOP Advanced OOP

L2 · Concept 01 · Advanced OOP

Let book types share code.
Then add their own behavior.

Our Mini Library Manager is growing to include electronic, printed, and audio books.

Learning goalUse a parent class and a child class so shared Book data and methods do not need to be copied.
01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

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 False

borrow() 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.

Add a new child ability
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.

Python Playground

Loading Python runtime…

Run the example, then create an AudioBook child class and add its own method.

inheritance.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

EBook inherits borrow() from Book.

class Book:
    def borrow(self):
        self.is_available = False

class EBook(Book):
    pass

ebook = EBook()
ebook.is_available = True
ebook.borrow()
print(ebook.is_available)
Choose the output

Practice independently

Mini Challenge — Play an audio sample

Finish AudioBook(Book) by adding one method without copying Book code.

Your goalDefine play_sample(self) to print a short sample message using self.title.

audiobook.py

Optional hints
  • Define play_sample(self) inside AudioBook.
  • Use self.title in the printed sample message.
Ask Sloby for a hint
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.