ProgramGuru

Composition vs Inheritance

Start with Composition vs Inheritance

Go favors composition—building types from fields, embedded types and small interfaces—over class-style inheritance.

What you’ll learn

  • Explain the main purpose of composition vs inheritance.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

With composition, a larger type can contain a reusable component and delegate work to it. Embedding can promote selectors, but it does not create a subtype relationship or provide virtual inheritance.

Interfaces supply substitutability by behavior: unrelated types can satisfy the same interface. This combination of composition and implicit interface implementation provides flexible designs without an extends keyword.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for compositionComposition reuses behavior without class inheritance.Engine componentStep 1Car embeds EngineStep 2Start promotedStep 3No subclass relationStep 4Composition reuses behavior without class inheritance.
Composition reuses behavior without class inheritance.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import "fmt"

type Engine struct{ Power int }

func (e Engine) Start() string { return "Engine started" }

type Car struct {
	Engine
	Model string
}

func main() {
	car := Car{Engine: Engine{Power: 120}, Model: "City"}
	fmt.Println(car.Model)
	fmt.Println(car.Start())
	fmt.Println("Power:", car.Engine.Power)
}

Output

City
Engine started
Power: 120

Trace what the program does

Car contains an embedded Engine value. Engine.Start is promoted, so car.Start() works, while car.Engine.Power still explicitly locates Power in the embedded component.

Avoid common mistakes

  • Embedding does not make Car inherit all type identity from Engine.
  • Prefer small reusable components instead of creating deep chains of embedding.

Try it yourself

Add a Radio component with an On method and embed it in Car. Demonstrate both promoted methods.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

Which statement describes struct embedding in Go?
Choose one answer. You can try again.

Answer and explanation

It composes types and may promote selectors

Correct. Car contains an embedded Engine value.

What you should remember

You have practiced Composition vs Inheritance in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Introduction to Error Handling →