OOP principle: generalisation

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

OOP principle: generalisation

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which statement best describes generalisation in OOP?
A Moving attributes and methods shared by several classes into one parent class
B Copying the same attributes and methods into every class that needs them
2. Dog, Cat and Bird classes each have name, age and a eat() method. What does generalisation suggest?
A Create an Animal parent class holding name, age and eat()
B Delete eat() from two of the classes so it only appears once
3. Which is the general class and which is the specialised class?
A Vehicle is general; Truck is specialised
B Truck is general; Vehicle is specialised
4. Where should a feature that is unique to only one class, such as Bird.wingspan, be placed?
A In the general Animal parent class
B In the specialised Bird class only
5. What is the main coding benefit of generalisation?
A It makes every class run faster because it has more attributes
B It reduces repeated code, so a change is made in one place

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1class Vehicle: AThe general class is the child of a specialised class. The specialised Student should inherit from the general Person.
2class Car(Vehicle): Bclass Animal: def __init__(self, name, age):
3Spotting common features: Dog and Cat both store a name and an age, so these shared attributes belong in a general Animal class Cclass Dog(Animal): def __init__(self, name, age): super().__init__(name, age)
4Reducing repeated code: a specialised class calls the general parent class to set up the shared attributes instead of copying them DA specialised class that reuses what Vehicle provides and only adds its own extra features
5class Student: def __init__(self, name): self.name = name class Teacher: def __init__(self, name): self.name = name EA general parent class holding the attributes and methods shared by Car, Truck and Bike
6class Person(Student): def __init__(self, name): self.name = name FBoth classes repeat the same name attribute. Generalisation would move it into a general Person parent class.

Definitions

generalisation

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
_____ Vehicle: def start(self): print("Engine on")
2.
class Vehicle: wheels = 4 class Car(_______): pass
3.
class Bike(Vehicle): ____ Bike().start()
4.
class Animal: ___ eat(self): print("Eating") class Cat(Animal): pass
5.
class Pet: legs = 4 class Dog(Pet): pass print(Dog().____)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A class Animal
B def __init__(self, name):
C self.name = name
D class Dog(Animal):
E pass
Question 2
A class Vehicle:
B def __init__(self, reg):
C reg = reg
D class Car(Vehicle):
E def __init__(self, reg):
F super().__init__(reg)
Question 3
A Class Shape:
B def __init__(self, col):
C self.col = col
D class Circle(Shape):
E pass
Question 4
A class Staff:
B def _init_(self, name):
C self.name = name
D class Teacher(Staff):
E def __init__(self, name):
F super().__init__(name)
Question 5
A class Pet:
B def speak():
C print('Hello')
D class Cat(Pet):
E pass

Answer Key

OOP principle: generalisation
Section A: Quick Questions
Q1
A — Moving attributes and methods shared by several classes into one parent class
Q2
A — Create an Animal parent class holding name, age and eat()
Q3
A — Vehicle is general; Truck is specialised
Q4
B — In the specialised Bird class only
Q5
B — It reduces repeated code, so a change is made in one place
Section B: Match the Pairs
Q1
class Vehicle: → A general parent class holding the attributes and methods shared by Car, Truck and Bike
Q2
class Car(Vehicle): → A specialised class that reuses what Vehicle provides and only adds its own extra features
Q3
Spotting common features: Dog and Cat both store a name and an age, so these shared attributes belong in a general Animal class → class Animal: def __init__(self, name, age):
Q4
Reducing repeated code: a specialised class calls the general parent class to set up the shared attributes instead of copying them → class Dog(Animal): def __init__(self, name, age): super().__init__(name, age)
Q5
class Student: def __init__(self, name): self.name = name class Teacher: def __init__(self, name): self.name = name → Both classes repeat the same name attribute. Generalisation would move it into a general Person parent class.
Q6
class Person(Student): def __init__(self, name): self.name = name → The general class is the child of a specialised class. The specialised Student should inherit from the general Person.
Section C: Definitions
Q1
generalisation — Finding the attributes and methods that several classes share and moving them into one more general parent class, so the code is not repeated in each specialised class.
Section D: Fill in the Blanks
Q1
class Vehicle: def start(self): print("Engine on")
Q2
class Vehicle: wheels = 4 class Car(Vehicle): pass
Q3
class Bike(Vehicle): pass Bike().start()
Q4
class Animal: def eat(self): print("Eating") class Cat(Animal): pass
Q5
class Pet: legs = 4 class Dog(Pet): pass print(Dog().legs)
Section E: Spot the Error
Q1
A — The general parent class header is missing its colon. It should be class Animal: so the shared name attribute can be defined inside it.
Q2
C — This only creates a local variable, so the shared attribute is never stored on the object. It should be self.reg = reg in the general parent class.
Q3
A — Python keywords are case sensitive. It must be a lowercase class, so the line should be class Shape: for the general parent class.
Q4
B — The constructor needs double underscores on each side. It should be def __init__(self, name): so the shared attribute is set up properly.
Q5
B — A method in the general class needs self as its first parameter. It should be def speak(self): so Cat can inherit it instead of repeating the code.