Publication Class Hierarchy

Implement a class Publication and then define child classes Book, Magazine, Newspaper, each with additional attributes like title and author for books, title and issue for magazines, and title and date for newspapers. Implement methods to print the details.

Example 1:
Input: publication = Book("1984", "George Orwell")
Output: Title: 1984, Author: George Orwell
Example 2:
Input: publication = Newspaper("The Times", "2023-08-02")
Output: Title: The Times, Date: 2023-08-02

Use inheritance to create child classes and override the method to return the specific details for each publication type.

class Publication:
    def details(self):
        pass

class Book(Publication):
    def __init__(self, title, author):
        self.title = title
        self.author = author

    def details(self):
        return f"Title: {self.title}, Author: {self.author}"

class Magazine(Publication):
    def __init__(self, title, issue):
        self.title = title
        self.issue = issue

    def details(self):
        return f"Title: {self.title}, Issue: {self.issue}"

class Newspaper(Publication):
    def __init__(self, title, date):
        self.title = title
        self.date = date

    def details(self):
        return f"Title: {self.title}, Date: {self.date}"

publication = Book("1984", "George Orwell")
print(publication.details())

publication = Newspaper("The Times", "2023-08-02")
print(publication.details())

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!