Quiz 12: How to work with inheritance
Consider the following code: class Product: def __init__(self, name="", price=0.0): self.name = name self.price = price def getDescription(self): return self.name class Book(Product): def __init__(self, name="", price=0.0, author=""): Product.__init__(self, name, price) self.author = author def getDescription(self): return Product.getDescription(self) + " by " + self.author class Movie(Product): def __init__(self, name="", price=0.0, year=0): Product.__init__(self, name, price) self.year = year def getDescription(self): return Product.getDescription(self) + " (" + str(self.year) + ")" What does this code print to the console: def display(product): print(product.getDescription()) book = Book("Catcher in the Rye", 9.99, "J. D. Salinger") display(book)
Catcher in the Rye by J. D. Salinger
Consider the following code: class Product: def __init__(self, name="", price=0.0): self.name = name self.price = price def getDescription(self): return self.name class Book(Product): def __init__(self, name="", price=0.0, author=""): Product.__init__(self, name, price) self.author = author def getDescription(self): return Product.getDescription(self) + " by " + self.author class Movie(Product): def __init__(self, name="", price=0.0, year=0): Product.__init__(self, name, price) self.year = year def getDescription(self): return Product.getDescription(self) + " (" + str(self.year) + ")" If a class named App inherits the Product class, you can add an attribute named version by coding the constructor for the App class like this:
NOT def __init__(self, name="", price=0.0, version="1.0"): App.__init__(self, name, price, version)
Consider the following code: class Product: def __init__(self, name="", price=0.0): self.name = name self.price = price def getDescription(self): return self.name class Book(Product): def __init__(self, name="", price=0.0, author=""): Product.__init__(self, name, price) self.author = author def getDescription(self): return Product.getDescription(self) + " by " + self.author class Movie(Product): def __init__(self, name="", price=0.0, year=0): Product.__init__(self, name, price) self.year = year def getDescription(self): return Product.getDescription(self) + " (" + str(self.year) + ")" The superclass is named
Product
Consider the following diagram: Which of the following is true?
The Book and Movie classes inherit the Product class.
If you have a class named Vehicle, and you want to code a class named Truck that inherits the Vehicle class, you can begin by writing this code:
class Truck : Vehicle
The object-oriented programming concept that allows you to define a new class that's based on an existing class is
inheritance
Which function provides a way to check an object's type?
isinstance()
If you create an object from the Movie class, which attribute(s) can you access from that object?
name, price, discountPercent, and year
The getDescription() method of the Book class _________ the getDescription() method of the Product class.
overrides
The feature of inheritance that allows an object of a subclass to be treated as if it were an object of the superclass is known as
polymorphism