MCS-201 · Unit 13

Classes in Python

Section 4 of 7

Block 4 · Unit 13Classes in Python8 min · Section 4 of 7

13.4Composition

Covers IGNOU MCS-201 §13.4

The heading of §13.4 is “Inheritance and Compositions”, and the programme guide lists composition, but the printed unit explains only inheritance. Composition is the other main way to reuse code, and it is used in real programs more often than inheritance.

What composition is

A Car has an Engine; a University has Departments; an Order has OrderLines. The containing object delegates work to its parts by calling their methods.

InheritanceComposition
Relationship“is-a”“has-a”
Howclass Child(Parent):self.part = Part(...) inside the class
ReuseChild gets all parent members automaticallyClass calls the part’s methods explicitly
CouplingTight: changes in the parent affect every childLoose: parts can be replaced or changed independently
ExampleSavingsAccount(Account)Car with an Engine

A common design rule is “prefer composition over inheritance”: use inheritance only when the child truly is a special kind of the parent and should support everything the parent does.

Key takeaways

  • Composition: a class holds other objects as attributes (“has-a”).
  • The outer class delegates to its parts by calling their methods.
  • Composition is loosely coupled; parts can be swapped.
  • Use inheritance only for true “is-a” relationships.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which relationship does composition express?

Show answer

Answer: B. has-a

A composed object contains its parts, as a car has an engine.

Question 2 · multiple choice

Which is the better design for “a library has many books”?

Show answer

Answer: C. A Library class with an attribute self.books = [] holding Book objects

A library is not a book, nor the reverse; it contains books, so composition fits.

Question 3 · multiple choice

What does this program print?

Python · quiz_compose.py
class Battery:
    def __init__(self, level):
        self.level = level

class Phone:
    def __init__(self, battery):
        self.battery = battery
    def status(self):
        return f"Battery {self.battery.level}%"

b = Battery(80)
p = Phone(b)
b.level = 45
print(p.status())
Show answer

Answer: B. Battery 45%

The phone holds a reference to the same battery object, so a change to b is seen through p.battery.

Question 4 · multiple choice

Which statement about composition is true?

Show answer

Answer: C. The outer class calls its parts’ methods explicitly

Composition reuses code by delegation, for example self.engine.start(); nothing is inherited.

Question 5 · multiple choice

Why is composition said to be “loosely coupled”?

Show answer

Answer: B. A part can be replaced by another object with the same methods without changing the outer class

In the example, any object with a start() method can be passed as the engine.

Question 6 · short answer

Design classes for “a college has departments, and each department has a head and a list of teachers”, stating which relationships are composition.

Show worked answer

Use a Teacher class (name, subject); a Department class whose constructor stores self.head (a Teacher) and self.teachers (a list of Teacher objects), with a method such as add_teacher(t); and a College class with self.departments (a list of Department objects). College has departments, department has a head and teachers: all three are “has-a” relationships, so all are composition. Inheritance would fit only a true “is-a” link, for example class Professor(Teacher).

↑ ↓ move↵ openTab switch type