IB Computer Science · B3 · Object-oriented programming
B3.2 OOP with multiple classes
Explain how classes interact through references, design related classes, and compare composition with inheritance.
1/6Classes cooperate by using each other’s objects
- Classes cooperate by using each other’s objects
- A reference lets one object reach another
- Composition builds one object from other objects
- Inheritance lets a subclass reuse a superclass
- A subclass can override an inherited method
- Composition and inheritance suit different relationships
- Check your recall
- Practice: 10 questions
Part 1 of 6: Classes cooperate by using each other’s objects
A library system needs books, members and loans. One class cannot hold all of that cleanly. Each class handles its own data, and the classes work together.
An association exists when objects of one class use objects of another. For example, a Loan object may ask a Book object for its title. It does this by calling a method of Book.
Key term
association
An association is a relationship in which objects of one class use or refer to objects of another class.
Part 2 of 6: A reference lets one object reach another
To call a method on another object, a class needs a way to find it. A reference identifies an object in memory. It works like an address that tells the program where to look. An attribute can hold a reference to another object.
Through that attribute, the first object calls methods of the second. Copying a reference does not copy the object. Two variables then refer to the same object.
Worked example
Copy a reference
- s1 holds a reference to one Student object.
- The assignment copies the reference, so s2 refers to the same object.
- setGrade changes the grade of that single object.
- Reading s1.grade looks at the same object.
Answer: s1.grade is 11.
Key term
reference
A reference is a value, held in a variable or attribute, that identifies where an object is stored in memory.

Quick check
One question on the part you just read
Easy
Show answer and explanation
Answer: A · Worked explanation
Part 3 of 6: Composition builds one object from other objects
Some objects are made of parts. A car contains an engine, and a car is not a kind of engine. This is a has-a relationship, which programs model with composition.
In composition, an attribute of the whole holds an object of another class. The whole usually creates its parts in its constructor. If the whole is discarded, its parts normally go with it.
Worked example
Design a Car with an Engine
- Engine has an attribute running, which its constructor sets to false.
- Engine has a method start, which sets running to true.
- Car has an attribute engine. Its constructor sets engine = new Engine().
- Car has a method start, which calls engine.start().
Answer: After c = new Car() and c.start(), the value of c.engine.running is true.
This design keeps each class small and focused. Car does not need to know how an engine starts, only that it can.
Key term
composition
Composition is a design in which a class has attributes that are objects of other classes, forming a has-a relationship.
Part 4 of 6: Inheritance lets a subclass reuse a superclass
Composition links separate classes. Some classes, however, share most of their features. Inheritance lets a new class take the attributes and methods of an existing class.
The existing class is the superclass. The new class is the subclass. A Car is a kind of Vehicle, so Car can be a subclass of Vehicle. This is an is-a relationship.
The subclass can add its own attributes and methods. Changes made in the superclass are then available to every subclass. Subclasses therefore avoid repeating code that the superclass already holds.
Key terms · tap one for its definition
inheritance
Inheritance is a mechanism by which a subclass acquires the attributes and methods of its superclass.
superclass
A superclass is a class whose attributes and methods are inherited by one or more subclasses.
subclass
A subclass is a class that inherits from a superclass and may add its own features or override inherited ones.

Quick check
One question on the part you just read
Easy
Show answer and explanation
Answer: B · Worked explanation
Part 5 of 6: A subclass can override an inherited method
A subclass often needs slightly different behaviour. To override a method, the subclass defines a method with the same name as an inherited one. Objects of the subclass use the new version. Overriding lets one call, such as move, behave differently for each kind of object.
Worked example
Override a method
- b refers to a Bicycle object.
- Bicycle defines its own move, so the program uses that version.
- The inherited version in Vehicle is not used for this object.
Answer: The output is pedalling.
Other subclasses keep the inherited version unless they override it too. A Car with no move of its own would output moving. Subclass constructors usually call the superclass constructor first, which sets up the inherited attributes.
Key term
override
An override is a method in a subclass, with the same name as an inherited method, that replaces the inherited version.
Part 6 of 6: Composition and inheritance suit different relationships
Both techniques reuse code, so you need a test to choose. Ask whether the new class is a kind of the old one, or contains it. Each suits a different kind of link between classes.
Inheritance compared with composition
- Feature
- Relationship
- Inheritance
- is-a: a Car is a Vehicle
- Composition
- has-a: a Car has an Engine
- Feature
- Reuse comes from
- Inheritance
- Inherited attributes and methods
- Composition
- Calling the parts’ methods
- Feature
- Link between classes
- Inheritance
- Close: subclass depends on superclass
- Composition
- Looser: a part can be replaced
- Feature
- Effect of a change
- Inheritance
- A superclass change affects all subclasses
- Composition
- A change in a part stays contained
If a Car is a kind of Vehicle, use inheritance. If a Car contains an Engine, use composition. Many designs use both together. Choosing well keeps the design easy to change.
Check your recall[15 marks]
6 written questions. Answer each one in your head or on paper, then open the mark scheme.
- 1.Describe[3 marks]
Describe how one object can use the methods of another object.
Show mark schemeHide mark scheme
Model answer
The first object holds a reference to the second in an attribute. It then calls the method using dot notation, such as part.start(). The second object runs the method on its own data.
- 1 mark for stating that an attribute holds a reference to the other object
- 1 mark for stating that a method is called through that reference using dot notation
- 1 mark for stating that the called object runs the method on its own data
- 2.Distinguish[2 marks]
Distinguish between a superclass and a subclass.
Show mark schemeHide mark scheme
Model answer
A superclass is the class whose attributes and methods are inherited. A subclass is the class that inherits them and may add or override features.
- 1 mark for stating that a superclass is the class that passes on its features
- 1 mark for stating that a subclass inherits them and may add or override features
- 3.Determine[2 marks]
Determine the value of p.grade after: p = new Student("Ana", 11), q = p, q.setGrade(12). Give a reason.
Show mark schemeHide mark scheme
Model answer
p.grade is 12. The assignment q = p copies the reference, so p and q refer to the same object and the change is seen through both.
- 1 mark for obtaining p.grade = 12
- 1 mark for explaining that q and p refer to the same object
- 4.Compare[4 marks]
Compare composition and inheritance as ways of relating classes.
Show mark schemeHide mark scheme
Model answer
Inheritance models an is-a relationship, and a subclass takes the attributes and methods of its superclass. Composition models a has-a relationship, where one class holds objects of another. Inheritance links classes closely, so a superclass change affects all subclasses. Composition is looser, so a part can be changed with less effect.
- 1 mark for stating that inheritance models is-a and composition models has-a
- 1 mark for stating that a subclass takes attributes and methods from its superclass
- 1 mark for stating that composition holds objects of another class as attributes
- 1 mark for comparing how closely linked the classes are or the effect of a change
- 5.Explain[2 marks]
Explain what it means for a subclass to override a method.
Show mark schemeHide mark scheme
Model answer
The subclass defines a method with the same name as one it inherits. Objects of the subclass then use the new version instead of the inherited one.
- 1 mark for stating that the subclass defines a method with the same name as an inherited method
- 1 mark for explaining that the subclass objects use the new version instead of the inherited one
- 6.Deduce[2 marks]
Deduce whether inheritance or composition suits the classes Bicycle and Wheel. Give a reason.
Show mark schemeHide mark scheme
Model answer
Composition suits them. A bicycle has wheels, and a bicycle is not a kind of wheel, so this is a has-a relationship.
- 1 mark for deducing composition
- 1 mark for giving the reason that a bicycle has wheels rather than being a kind of wheel

Practice this topic
10 quick-fire multiple-choice questions, easy to hard. Tap an answer to see if you're right and the worked explanation. (The written questions with mark schemes are under “Check your recall” above.)
0 right0 of 10 answered
Includes the 2 quick checks in the note above.
EasyIdentify[1 mark]
Show answer and explanation
Answer: C · Worked explanation
MediumPredict[1 mark]
Show answer and explanation
Answer: D · Worked explanation
MediumIdentify[1 mark]
Show answer and explanation
Answer: A · Worked explanation
MediumIdentify[1 mark]
Show answer and explanation
Answer: B · Worked explanation
MediumCalculate[1 mark]
Show answer and explanation
Answer: C · Worked explanation
HardExplain[2 marks]
Show answer and explanation
Answer: D · Worked explanation
HardExplain[2 marks]
Show answer and explanation
Answer: B · Worked explanation
HardDetermine[2 marks]
Show answer and explanation
Answer: C · Worked explanation