Loading...
An object-oriented relationship should describe the problem rather than merely reuse code. Distinguish “is a kind of” inheritance from relationships in which one object contains or uses another.
What you'll be able to do
Inheritance specialises a base class, representing an appropriate is-a relationship. Aggregation relates objects that can exist independently, such as a team and a player. Composition expresses stronger ownership: a component’s conceptual lifecycle belongs to its whole. UML uses a hollow diamond for aggregation and a filled diamond for composition, both at the whole end.
A design can favour composition when behaviour should be assembled from replaceable collaborators rather than forced into a deep inheritance hierarchy. Encapsulating what varies reduces changes spreading through unrelated classes. Reuse alone is insufficient evidence that an is-a relationship is sensible.
Polymorphism allows code using a shared interface to work with different concrete implementations. A subclass can override an inherited method so that the method selected at runtime reflects the actual object. The caller should depend on the promised behaviour rather than inspect every implementation’s internal fields.
An abstract method specifies an operation that a concrete subclass must provide. A virtual method permits runtime overriding in languages using that term. Python normally dispatches instance methods dynamically; C# and Java have their own rules. A static method belongs to the class interface without requiring an instance receiver.
A class diagram lists attributes, operations and relationships, including visibility markers and single-inheritance links. Draw the generalisation arrow towards the more general class. For composition, the diamond marks the owning whole, not the contained part.
Programming to an interface makes replacement easier when implementations obey the same contract. AQA includes awareness of this design principle but does not require explicit interface programming in the practical examination. Keep language-independent design ideas distinct from the specific syntax used in a classroom language.
class Shape:
def area(self):
raise NotImplementedError
class Square(Shape):
def __init__(self, side):
self.side = side
def area(self):
return self.side * self.side
class Rectangle(Shape):
def __init__(self, width, height):
self.width, self.height = width, height
def area(self):
return self.width * self.height
print([shape.area() for shape in [Square(3), Rectangle(2, 4)]])Scroll horizontally to see the full code or table.from abc import ABC, abstractmethod
class Converter(ABC):
@abstractmethod
def convert(self, value):
pass
@staticmethod
def unit_label():
return "m"
class Double(Converter):
def convert(self, value):
return value * 2
print(Double().convert(3), Converter.unit_label())Scroll horizontally to see the full code or table.Attempt the task before opening its worked response. Record each change of state, explain the rule used, and check the result against the original requirements. Code examples labelled Python 3 use that language’s conventions; pseudocode states its own assumptions.
Common mistakes to avoid
Key takeaways
Test yourself
Answer a few questions, then review anything you missed.
Start with the quiz above to check the key ideas. You can also choose another topic from the practice library.