Which pair of classes is the best fit for an inheritance (superclass/subclass) relationship?
ACar and Engine
BLibrary and Book
CShape and Circle
DStudent and Classroom
Inheritance models an 'is-a' relationship. A Circle is a Shape, so Circle can extend Shape. A Car has an Engine, a Library has Books, and a Classroom has Students; those are 'has-a' relationships, which are modeled by storing one object inside another (composition), not by inheritance.
Question 2 Multiple Choice
In Python, class Animal has an __init__ that sets self.name. Class Dog(Animal) defines its own __init__(self, name, breed) that sets only self.breed and never calls super().__init__(name). What happens when code later reads my_dog.name?
AIt returns the name, because Dog inherits Animal's attributes automatically
BIt raises an AttributeError, because Animal's __init__ never ran, so name was never set
CIt returns None, because inherited attributes default to None
DIt returns the breed, because Dog's __init__ replaces name with breed
Defining __init__ in Dog replaces Animal's __init__ for Dog objects. Attributes in Python are created when code assigns them, so if Animal's __init__ never runs, self.name is never assigned. Calling super().__init__(name) inside Dog's __init__ runs the superclass setup and fixes the problem.
Question 3 True / False
In Java, a subclass can directly read and assign a private instance variable declared in its superclass.
TTrue
FFalse
Answer: False
Private members are visible only inside the class that declares them. A subclass object still contains that data, but subclass code must reach it through the superclass's public or protected methods (or a protected field), not by name.
Question 4 Short Answer
Two classes, Teacher and Student, both store a name and an email address and both have an identical send_email method. Explain how inheritance improves this design.
Think about your answer, then reveal below.
Model answer: Create a superclass such as Person that holds name, email and send_email. Teacher and Student then extend Person and add only what is specific to each. The shared code exists once, so a bug fix or change to send_email happens in one place and applies to both subclasses.
Removing duplicated code is the main practical benefit of inheritance. It also passes the 'is-a' test: a Teacher is a Person and a Student is a Person.