45. Problem to create an inheritance example in Python

In this Python oops program, we will create an inheritance example in Python, Python class called EBook that inherits from the Book class.

Inheritance example in Python

Steps to solve the program
  1. The Book class is defined with a constructor method __init__ that takes title, author, and pages as parameters and initializes the instance variables self.title, self.author, and self.pages with the provided values.
  2. The class also has three methods: get_title, get_author, and get_pages.
  3. The get_title method returns the title of the book.
  4. The get_author method returns the author of the book.
  5. The get_pages method returns the number of pages in the book.
  6. The EBook class is defined as a subclass of the Book class. It inherits the attributes and methods of the Book class.
  7. The __init__ method of the EBook class extends the functionality of the Book class’s __init__ method by adding two additional parameters: file_size and format. It initializes the instance variables self.file_size and self.format with the provided values.
  8. The open_book method prints a message indicating the opening of the e-book.
  9. The close_book method prints a message indicating the closing of the e-book.
  10. The code creates an object ebook of the EBook class by calling its constructor and passing the title “Harry Potter”, author “J.K. Rowling”, number of pages 300, file size “10MB”, and format “PDF” as arguments.
  11. The get_title, get_author, and get_pages methods inherited from the Book class are called on the ebook object to retrieve the respective book details, which are then printed.
  12. The file_size and format attributes specific to the EBook class are accessed directly from the ebook object and printed.
  13. The open_book method is called on the ebook object to simulate opening the e-book, and a corresponding message is printed.
  14. The close_book method is called on the ebook object to simulate closing the e-book, and a corresponding message is printed.
  15. Thus, we have created an inheritance example in Python.
				
					class Book:
    def __init__(self, title, author, pages):
        self.title = title
        self.author = author
        self.pages = pages
    
    def get_title(self):
        return self.title
    
    def get_author(self):
        return self.author
    
    def get_pages(self):
        return self.pages



class EBook(Book):
    def __init__(self, title, author, pages, file_size, format):
        super().__init__(title, author, pages)
        self.file_size = file_size
        self.format = format
    
    def open_book(self):
        print("Opening the e-book")
    
    def close_book(self):
        print("Closing the e-book")

# Create an object of the EBook class
ebook = EBook("Harry Potter", "J.K. Rowling", 300, "10MB", "PDF")
print("Title:", ebook.get_title())
print("Author:", ebook.get_author())
print("Pages:", ebook.get_pages())
print("File Size:", ebook.file_size)
print("Format:", ebook.format)
ebook.open_book()
ebook.close_book()
				
			

Output:

				
					Title: Harry Potter
Author: J.K. Rowling
Pages: 300
File Size: 10MB
Format: PDF
Opening the e-book
Closing the e-book
				
			

Related Articles

Create a Python class called Book with attributes title, author, and pages.

Create a Python class called ShoppingCart with attributes items and total_cost.

Leave a Comment