---
title: "Object Composition — AP Comp Sci A Definition & Examples"
description: "Object composition means a class stores objects of other classes as instance variables (a 'has-a' relationship). Core to Unit 3 class design on AP CSA."
canonical: "https://fiveable.me/ap-comp-sci-a/key-terms/object-composition"
type: "key-term"
subject: "AP Computer Science A"
unit: "Unit 3"
---

# Object Composition — AP Comp Sci A Definition & Examples

## Definition

In AP Computer Science A, object composition is a class design pattern where one class holds references to objects of other classes as instance variables, creating a 'has-a' relationship (a Car has an Engine). It's part of Topic 3.3, Anatomy of a Class.

## What It Is

Object composition is what happens when a [class](/ap-comp-sci-a/unit-3/abstraction-and-program-design/study-guide/o9VgVeIpKRYZ7N7rXfUz "fv-autolink")'s [instance variables](/ap-comp-sci-a/key-terms/instance-variables "fv-autolink") aren't just ints and Strings. They're objects of other classes. A `Course` class might store a `Teacher` object and an `ArrayList<Student>`. A `Car` might store an `Engine`. The shorthand you'll hear constantly is the **'has-a' relationship**. A Car *has an* Engine, so Engine becomes one of Car's attributes.

In the CED, this lives in [Topic 3.3](/ap-comp-sci-a/unit-3/anatomy-of-a-class/study-guide/DcGY5KOyK98H9Fn2w8jh "fv-autolink") (Anatomy of a Class) because composed objects are just instance variables like any others. They belong to the object, they're declared `private` for encapsulation (EK 3.3.A.1), and they get initialized in the constructor. The only difference is that their type is a class you (or Java) wrote, so the containing class can call that object's methods to get work done. That's the real power move. Instead of one giant class doing everything, you build small classes and snap them together like LEGO bricks.

## Why It Matters

Composition is the backbone of [Unit 3](/ap-comp-sci-a/unit-3 "fv-autolink") (Class Creation) and directly supports learning objective [AP Comp Sci A](/ap-comp-sci-a "fv-autolink") 3.3.A, which asks you to develop code that controls access and visibility for classes, data, constructors, and methods. When a class holds another object as a private instance variable, you're applying data encapsulation (EK 3.3.A.1) to an object-typed attribute. Outside code can't touch the Engine inside the Car directly; it has to go through Car's public methods. That hiding of implementation details is exactly what the EK describes. Composition is also how every realistic FRQ class works. Almost any 'write a class' prompt gives you a class that stores other objects (a roster of Students, a list of Books) and asks you to write methods that use them.

## Connections

### [Attribute (Unit 3)](/ap-comp-sci-a/key-terms/attribute)

Composition is just a special case of an [attribute](/ap-comp-sci-a/key-terms/attribute "fv-autolink"). When an attribute's type is a class instead of a primitive, you have composition. Same declaration syntax, same private visibility, same spot in the class anatomy.

### Data encapsulation (Unit 3, Topic 3.3)

EK 3.3.A.1 says implementation details stay hidden from external classes. Composed [objects](/ap-comp-sci-a/key-terms/object "fv-autolink") are usually private instance variables, so the outside world interacts with them only through the containing class's public methods. Composition and encapsulation work as a team.

### [Behavior (Unit 3)](/ap-comp-sci-a/key-terms/behavior)

A composed object brings its [behaviors](/ap-comp-sci-a/key-terms/behavior "fv-autolink") with it. Instead of Car re-implementing engine logic, Car's methods call engine.start(). This is delegation, and it's why composition keeps each class short and focused.

### Constructors (Unit 3, Topic 3.3)

Object-typed instance variables must be initialized in the constructor just like primitives, either from a parameter (this.engine = e) or by calling new. Forgetting this leaves the reference null, which is a classic FRQ point-loser.

## On the AP Exam

You won't see a multiple-choice stem that says 'define object composition.' Instead, the exam hands you composition and expects you to read and write it fluently. MCQs show a class with object-typed instance variables and ask what a method call does or which line breaks encapsulation. On the FRQs, the class design question (and often the methods question) gives you a class that contains other objects, like a `Library` holding an `ArrayList<Book>`, and asks you to write a constructor that initializes them and methods that call their methods. The skills being scored are declaring private instance variables, initializing them in a constructor, and calling methods on the contained objects. Miss the `private`, and you lose encapsulation points under LO 3.3.A.

## object composition vs Inheritance

Both connect two classes, but the relationship is different. Composition is 'has-a' (a Car has an Engine, stored as an instance variable). Inheritance is 'is-a' (a SportsCar is a Car, using extends). With composition, the outer class accesses the inner object through method calls on a reference. With inheritance, the subclass directly receives the parent's members. Quick test for any prompt: if you can say 'X has a Y,' use composition; if 'X is a Y,' that's inheritance.

## Key Takeaways

- Object composition means a class stores objects of other classes as instance variables, creating a 'has-a' relationship.
- Composed objects are declared as private instance variables, which is data encapsulation in action (EK 3.3.A.1).
- Object-typed instance variables must be initialized in the constructor, either from a parameter or with the new keyword, or they stay null.
- The containing class uses the composed object by calling its public methods, like car's methods calling engine.start().
- Composition is 'has-a' while inheritance is 'is-a'; use the sentence test to tell them apart on any design question.
- Nearly every AP CSA class-writing FRQ uses composition, so practice writing classes that hold and use other objects.

## FAQs

### What is object composition in AP Computer Science A?

It's a class design pattern where a class contains references to objects of other classes as instance variables, like a Course class storing a Teacher object. It models a 'has-a' relationship and falls under Topic 3.3, Anatomy of a Class, in Unit 3.

### Is object composition the same as inheritance?

No. Composition is a 'has-a' relationship where one class stores another as an instance variable. Inheritance is an 'is-a' relationship using extends. A Car has an Engine (composition); a SportsCar is a Car (inheritance).

### Do composed objects have to be private?

In AP CSA, yes, treat them that way. EK 3.3.A.1 says implementation details should be hidden from external classes, and instance variables (including object references) are declared private so outside code accesses them only through public methods.

### How do I initialize an object instance variable in a constructor?

Two common ways. Assign a parameter (this.engine = e) or create the object yourself (this.engine = new Engine()). If you skip both, the reference is null and calling a method on it throws a NullPointerException.

### Will object composition be on the AP CSA exam?

Yes, constantly, just not by name. MCQs show classes containing other objects and ask you to trace method calls, and the class-design FRQ almost always requires writing a class that holds and uses objects of other classes, like an ArrayList of Student objects.

## Related Study Guides

- [3.3 Anatomy of a Class](/ap-comp-sci-a/unit-3/anatomy-of-a-class/study-guide/DcGY5KOyK98H9Fn2w8jh)

## Structured Data

```json
{"@context":"https://schema.org","@graph":[{"@type":"LearningResource","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/object-composition#resource","name":"Object Composition — AP Comp Sci A Definition & Examples","url":"https://fiveable.me/ap-comp-sci-a/key-terms/object-composition","learningResourceType":"Concept explainer","educationalLevel":"AP® / High School","about":{"@id":"https://fiveable.me/ap-comp-sci-a/key-terms/object-composition#term"},"audience":{"@type":"EducationalAudience","educationalRole":"student"},"dateModified":"2026-06-11T05:27:18.696Z","isPartOf":{"@type":"Collection","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"},"publisher":{"@type":"Organization","name":"Fiveable","url":"https://fiveable.me"}},{"@type":"DefinedTerm","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/object-composition#term","name":"object composition","description":"In AP Computer Science A, object composition is a class design pattern where one class holds references to objects of other classes as instance variables, creating a 'has-a' relationship (a Car has an Engine). It's part of Topic 3.3, Anatomy of a Class.","url":"https://fiveable.me/ap-comp-sci-a/key-terms/object-composition","inDefinedTermSet":{"@type":"DefinedTermSet","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"}},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is object composition in AP Computer Science A?","acceptedAnswer":{"@type":"Answer","text":"It's a class design pattern where a class contains references to objects of other classes as instance variables, like a Course class storing a Teacher object. It models a 'has-a' relationship and falls under Topic 3.3, Anatomy of a Class, in Unit 3."}},{"@type":"Question","name":"Is object composition the same as inheritance?","acceptedAnswer":{"@type":"Answer","text":"No. Composition is a 'has-a' relationship where one class stores another as an instance variable. Inheritance is an 'is-a' relationship using extends. A Car has an Engine (composition); a SportsCar is a Car (inheritance)."}},{"@type":"Question","name":"Do composed objects have to be private?","acceptedAnswer":{"@type":"Answer","text":"In AP CSA, yes, treat them that way. EK 3.3.A.1 says implementation details should be hidden from external classes, and instance variables (including object references) are declared private so outside code accesses them only through public methods."}},{"@type":"Question","name":"How do I initialize an object instance variable in a constructor?","acceptedAnswer":{"@type":"Answer","text":"Two common ways. Assign a parameter (this.engine = e) or create the object yourself (this.engine = new Engine()). If you skip both, the reference is null and calling a method on it throws a NullPointerException."}},{"@type":"Question","name":"Will object composition be on the AP CSA exam?","acceptedAnswer":{"@type":"Answer","text":"Yes, constantly, just not by name. MCQs show classes containing other objects and ask you to trace method calls, and the class-design FRQ almost always requires writing a class that holds and uses objects of other classes, like an ArrayList of Student objects."}}]},{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"AP Computer Science A","item":"https://fiveable.me/ap-comp-sci-a"},{"@type":"ListItem","position":2,"name":"Key Terms","item":"https://fiveable.me/ap-comp-sci-a/key-terms"},{"@type":"ListItem","position":3,"name":"Unit 3","item":"https://fiveable.me/ap-comp-sci-a/unit-3"},{"@type":"ListItem","position":4,"name":"object composition"}]}]}
```
