Scribe logoScribeNotes & practice
Sign inStart free
  1. Notes
  2. ›IB Computer Science
  3. ›B3 Object-oriented programming
  4. ›B3.2 OOP with multiple classes

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.

  • 5 min read
  • 6 parts
  • 10 questions
  • Draft
  • HL only
Start a Quick 5 on this topic→Jump to the 10 questions
1/6Classes cooperate by using each other’s objects
  1. 1Classes cooperate by using each other’s objects
  2. 2A reference lets one object reach another
  3. 3Composition builds one object from other objects
  4. 4Inheritance lets a subclass reuse a superclass
  5. 5A subclass can override an inherited method
  6. 6Composition and inheritance suit different relationships
  7. Check your recall
  8. Practice: 10 questions
1

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.

Exam tip

When describing interaction, name both classes and the method call that links them. For example, Loan calls getTitle on Book.

Key term

association

An association is a relationship in which objects of one class use or refer to objects of another class.

Part 1 done

2

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

Run: s1 = new Student("Ben", 10), then s2 = s1, then s2.setGrade(11). State s1.grade.
  1. 1
    s1 holds a reference to one Student object.
  2. 2
    The assignment copies the reference, so s2 refers to the same object.
  3. 3
    setGrade changes the grade of that single object.
  4. 4
    Reading s1.grade looks at the same object.

Answer: s1.grade is 11.

Common mistake

Assigning one object variable to another does not make a new object. Both names refer to the same object.

Key term

reference

A reference is a value, held in a variable or attribute, that identifies where an object is stored in memory.

Part 2 done

Quick check

One question on the part you just read

Easy

Identify what a reference to an object does.
Show answer and explanation

Answer: A · Worked explanation

A reference identifies an object, so a variable or attribute holding it can reach the object and call its methods. Copying the reference does not copy the object. A reference does not create subclasses or alter a constructor, which are matters of class design rather than of reaching an object.
3

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

Design classes so that a Car has an Engine, and starting the car starts its engine.
  1. 1
    Engine has an attribute running, which its constructor sets to false.
  2. 2
    Engine has a method start, which sets running to true.
  3. 3
    Car has an attribute engine. Its constructor sets engine = new Engine().
  4. 4
    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 3 done

4

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.

Figure 1. Car and Bicycle inherit colour and move from Vehicle, and each adds its own attribute and method.

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.

Part 4 done

Quick check

One question on the part you just read

Easy

Identify the relationship that inheritance models.
Show answer and explanation

Answer: B · Worked explanation

Inheritance models a subclass that is a kind of its superclass, so a Car is a Vehicle. A has-a relationship describes composition instead. Copying a reference and the order of creation are not relationships between classes at all, so they cannot be what inheritance describes.
5

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

Vehicle has a method move that outputs moving. Bicycle extends Vehicle and overrides move to output pedalling. Predict the output of b.move() where b = new Bicycle().
  1. 1
    b refers to a Bicycle object.
  2. 2
    Bicycle defines its own move, so the program uses that version.
  3. 3
    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 5 done

6

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

FeatureInheritanceComposition
Relationshipis-a: a Car is a Vehiclehas-a: a Car has an Engine
Reuse comes fromInherited attributes and methodsCalling the parts’ methods
Link between classesClose: subclass depends on superclassLooser: a part can be replaced
Effect of a changeA superclass change affects all subclassesA change in a part stays contained
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.

Exam tip

Justify a design by naming the relationship, is-a or has-a, and one consequence for changing the code.

Note read

Check your recall[15 marks]

6 written questions. Answer each one in your head or on paper, then open the mark scheme.

  1. 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. 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. 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. 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. 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. 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.

3.

Easy·Identify[1 mark]

Identify the name of the class that passes its attributes and methods on to other classes.
Show answer and explanation

Answer: C · Worked explanation

The superclass is the class whose features are inherited. The subclass is the receiving class, so it is the opposite role. A constructor is a method that sets up objects, and an association is a relationship in which objects of one class use objects of another.
4.

Medium·Predict[1 mark]

Vehicle has a method move that outputs moving. Bicycle extends Vehicle and overrides move to output pedalling. Predict the output of b.move() where b = new Bicycle().
Show answer and explanation

Answer: D · Worked explanation

Bicycle objects use the overriding version, so the output is pedalling. The output moving would come from the inherited version, which the override replaces. Both lines would need the overriding method to call the inherited one as well. Defining the same method name in a subclass is exactly what overriding means, so no error occurs.
5.

Medium·Identify[1 mark]

A Car has an attribute engine that holds an Engine object, and Engine has a method start. Identify the statement that starts the engine of the Car object c directly.
Show answer and explanation

Answer: A · Worked explanation

Dot notation reads from left to right: c is the Car, c.engine is the Engine object it holds, and start is the method called on that object. The other statements put the names in an order that does not follow this chain of references, so they do not reach a method of the Engine inside c.
6.

Medium·Identify[1 mark]

A Phone class has an attribute that holds a Battery object. Identify the relationship between the classes.
Show answer and explanation

Answer: B · Worked explanation

The Phone holds a Battery as an attribute, which is a has-a relationship, so composition fits. A phone is not a kind of battery and a battery is not a kind of phone, so neither inheritance direction suits. Overriding applies only when a subclass redefines an inherited method, and no subclass exists here.
7.

Medium·Calculate[1 mark]

Account has a method deposit(x) that adds x to balance. The program runs: a = new Account(100), b = a, b.deposit(30), a.deposit(20). Calculate a.balance.
Show answer and explanation

Answer: C · Worked explanation

The statement b = a copies the reference, so one object is used throughout. Its balance becomes 100+30+20=150100+30+20=150100+30+20=150. The value 120 and the value 130 come from treating a and b as separate objects that each receive only their own deposit. The value 100 ignores both deposits.
8.

Hard·Explain[2 marks]

Vehicle has a constructor that sets colour, and Car extends Vehicle and adds doors. Explain why the Car constructor usually calls the Vehicle constructor.
Show answer and explanation

Answer: D · Worked explanation

The inherited attribute colour belongs to every Car, and the Vehicle constructor is the code that gives it a value. Calling it first sets up that part, and then Car sets doors. Calling it does not stop inheritance or force any override, and no separate Vehicle object is created or deleted.
9.

Hard·Explain[2 marks]

A designer makes Engine a subclass of Car because both have a power value. Explain the best criticism of this design.
Show answer and explanation

Answer: B · Worked explanation

Inheritance is for an is-a relationship, and an engine is a part of a car, not a kind of car, so composition suits better. Sharing an attribute is not enough reason to inherit. The other statements describe rules that do not exist, since subclasses can have any attributes and need not have more methods.
10.

Hard·Determine[2 marks]

Box has a constructor Box(s) that sets size to s, and a method grow(x) that adds x to size. The program runs: x = new Box(5), y = new Box(x.size), z = y, z.grow(3). Determine x.size and y.size.
Show answer and explanation

Answer: C · Worked explanation

The second constructor receives the value 5, so y is a separate object. The statement z = y copies a reference, so z.grow(3) changes the object that y also refers to, giving 5+3=85+3=85+3=8. The object x is never changed. The answer of 8 for both assumes that x and y refer to one object.

Keep going

Next topicB4.1 Abstract data typesPrevious topicB3.1 OOP with a single class

More in B3 · Object-oriented programming

  1. B3.1OOP with a single classNotes · 10 questions
  2. B3.2OOP with multiple classesNotes · 10 questions· HL only

All IB Computer Science topics →

Draft: written by Scribe and checked automatically, not yet by a subject reviewer.

Practice questions are original, written by Scribe and checked automatically for correctness.

Topic codes follow the official numbering. Not endorsed by IB Diploma Programme or any exam board.

On this page

0/6 read

  1. 1Classes cooperate by using each other’s objects
  2. 2A reference lets one object reach another
  3. 3Composition builds one object from other objects
  4. 4Inheritance lets a subclass reuse a superclass
  5. 5A subclass can override an inherited method
  6. 6Composition and inheritance suit different relationships
  7. Check your recall
  8. Practice: 10 questions
All coursesScribeHow it worksPricingPrivacyTerms

Notes and questions are written by Scribe in our own words. Topic codes follow the official course numbering so you can find your place. Not produced or endorsed by the IB, College Board, AQA, OCR, Pearson Edexcel or any exam board.

© 2026 Scribe