Python · 4.2
Inheritance and Polymorphism
Name
Class
Date
Mark / 33
Section A
Circle the letter of the correct answer. One mark each.
- 1What does inheritance allow?1
- AA class to take on the attributes and methods of an existing class
- BTwo objects to share the same data
- CA method to have several names
- DA class to be deleted automatically
- 2What does
super().__init__(name, age)do?1- ACreates a new parent object
- BCalls the parent class's constructor
- CRenames the class
- DDeletes the parent class
- 3What is overriding?1
- ACalling a method twice
- BCreating two objects with the same name
- CRedefining a parent's method in a subclass
- DDeleting a method
- 4What is polymorphism?1
- AHiding data inside a class
- BObjects of different classes responding to the same method call in their own way
- CCreating many objects from one class
- DA class inheriting from two parents
- 5A Car and an Engine are best related by:1
- AThey cannot be related
- BThey should be the same class
- CComposition, because a Car has an Engine
- DInheritance, because a Car is an Engine
- 6What happens if a subclass does not define a method the parent has?1
- AThe object cannot be created
- BAn error occurs
- CThe parent's version is used
- DThe method does nothing
- 7Why is polymorphism useful when adding a new type of object to a game?1
- AIt makes the game run faster
- BIt removes the need for classes
- CIt uses less memory
- DExisting loops need no changes at all
- 8What is an abstract class?1
- AA class with no methods
- BA class with private attributes
- CA class stored in a separate file
- DA class that cannot be instantiated and exists only to be inherited from
- 9Which is NOT one of the four pillars of OOP?1
- AIteration
- BEncapsulation
- CPolymorphism
- DAbstraction
- 10What is the main benefit of putting shared behaviour in a parent class?1
- ASubclasses run faster
- BIt is written once, so a fix there fixes it in every subclass
- CIt prevents inheritance
- DIt uses less memory
Section B
Answer in the space provided. The number of marks tells you how many separate points to make.
- 1ExplainExplain what is meant by inheritance in object oriented programming.3
- 2ExplainExplain what polymorphism is and why it is useful, using an example.4
- 3WriteA program has an Animal class with a speak method. Write a Dog subclass that overrides speak, and explain what overriding means.4
- 4ExplainExplain the difference between inheritance and composition, and state when each should be used.4
- 5NameName and briefly explain the four principles of object oriented programming.8
Mark scheme
Section A
Section B
1 Explain what is meant by inheritance in object oriented programming. 3
One mark for each of the following, to a maximum of 3.
- A new class is based on an existing class
- It takes on the attributes and methods of that class
- It can add new members or override existing ones
Model answer. Inheritance is a relationship in which a new class, the subclass, is based on an existing class, the superclass. The subclass automatically takes on all the attributes and methods defined in the superclass, so shared behaviour only has to be written once. The subclass can then add attributes and methods of its own, and it can override any inherited method by defining its own version, which is then used in place of the parent's when called on a subclass object.
2 Explain what polymorphism is and why it is useful, using an example. 4
One mark for each of the following, to a maximum of 4.
- Objects of different classes respond to the same method call
- Each provides its own implementation of that method
- Gives a valid example such as a list of shapes or animals
- Explains that existing code does not need changing when a new class is added
Model answer. Polymorphism means that objects of different classes can be used through the same interface, with each one responding to a method call in its own way. For example, a list might contain Circle, Square and Triangle objects, all inheriting from a Shape class, and a loop can simply call area() on each without ever asking what type it is holding, because each class provides its own version of the method. This is useful because it removes the need for long chains of type checks. Without polymorphism, every loop that processed shapes would need an if statement for each type, and adding a new shape would mean finding and editing every one of those loops. With polymorphism, a new shape is added by writing one new class, and all the existing code continues to work unchanged.
3 A program has an Animal class with a speak method. Write a Dog subclass that overrides speak, and explain what overriding means. 4
One mark for each of the following, to a maximum of 4.
- Defines the subclass inheriting from Animal
- Defines a speak method with the same name
- Returns or prints a dog specific sound
- Explains that the subclass version replaces the parent version for objects of that class
Model answer. class Dog(Animal): def speak(self): return "Woof" Overriding means defining a method in a subclass that has the same name as one in its parent class. When that method is called on an object of the subclass, Python uses the subclass version rather than the inherited one, so a Dog object returns Woof while another Animal subclass returns something different. The parent's version is not removed and can still be reached using super().speak() if the subclass wants to extend the original behaviour rather than replace it entirely.
4 Explain the difference between inheritance and composition, and state when each should be used. 4
One mark for each of the following, to a maximum of 4.
- Inheritance expresses an is a relationship
- Composition expresses a has a relationship, where an object contains another
- Use inheritance when the subclass genuinely is a kind of the parent
- Use composition when one object simply uses or contains another
Model answer. Inheritance expresses an is a relationship: a Dog is a kind of Animal, so making Dog a subclass of Animal makes sense and Dog inherits everything an Animal can do. Composition expresses a has a relationship, where one object holds another as an attribute: a Car has an Engine, so the Car class creates an Engine object and stores it. The test for which to use is whether the sentence with is a makes sense. A Car is not a kind of Engine, so inheriting from Engine would produce a class hierarchy that is confusing and would give Car methods that make no sense for it. Choosing the wrong one is a common source of class hierarchies that nobody can follow, so the general advice is to prefer composition unless the is a relationship is genuinely true.
5 Name and briefly explain the four principles of object oriented programming. 8
One mark for each of the following, to a maximum of 8.
- Abstraction is exposing only what is needed
- Abstraction hides underlying complexity from the user of a class
- Encapsulation is bundling data with the methods that act on it
- Encapsulation controls access so data stays valid
- Inheritance is a class taking on the attributes and methods of another
- Inheritance avoids duplication because shared code is written once
- Polymorphism is different classes responding to the same call in their own way
- Polymorphism means new classes can be added without changing existing code
Model answer. Abstraction means exposing only what is needed to use a class while hiding the complexity behind it. Someone using a list does not need to know how it stores items in memory, only that append adds one, and a well designed class works the same way. Encapsulation means bundling an object's data together with the methods that operate on it, and controlling how that data can be accessed from outside. Because changes go through the class's own methods, it can validate them and guarantee that the object never enters an invalid state, such as a bank account with a negative balance. Inheritance means a class can be built from an existing one, automatically taking on its attributes and methods and then adding or overriding whatever it needs. This avoids duplication, because behaviour shared by many classes is written once in the parent, and a correction made there applies everywhere at once. Polymorphism means that objects of different classes can respond to the same method call, each in its own way. A loop can call update on every object in a list without knowing or caring what type each one is, and because of this a new type can be added simply by writing a new class, with no change at all to the code that already uses them. Together these four principles produce code that is easier to reuse, easier to extend and far easier to keep correct as it grows.