What is OOP?

IB Syllabus: B3.1 – Understand the fundamental concepts of the object-oriented programming paradigm.

Table of Contents

  1. Key Concepts
    1. The Problem with Procedural Code
    2. The OOP Solution
    3. The Four Principles of OOP
      1. 1. Encapsulation
      2. 2. Inheritance
      3. 3. Polymorphism
      4. 4. Abstraction
    4. Key Terminology
    5. When to Use OOP
  2. Worked Examples
    1. Example 1: Procedural vs OOP Thinking
    2. Example 2: Identifying the Four Principles
  3. Quick Check
  4. Fill in the Blanks
  5. Practice Exercises
    1. Core
    2. Extension
    3. Challenge
  6. Connections

Key Concepts

The Problem with Procedural Code

In procedural programming, you write a sequence of instructions that operate on data stored in separate variables. This works fine for small programs, but as programs grow, problems emerge:

// Procedural approach: managing student data
String student1Name = "Alice";
int student1Grade = 92;
String student2Name = "Bob";
int student2Grade = 85;
String student3Name = "Charlie";
int student3Grade = 78;

// What if we need 200 students?
// What if we add email, attendance, GPA?
// What if two functions both modify the same grade variable?

The data (names, grades) and the operations on that data (calculate average, check pass/fail) are separate. Any function can access and modify any variable. As the program grows, it becomes difficult to track which function changes which data, and bugs become hard to find.

The OOP Solution

Object-oriented programming (OOP) solves this by bundling related data and the operations that act on that data into self-contained units called objects. Each object is responsible for managing its own state.

// OOP approach: each student is a self-contained object
Student alice = new Student("Alice", 92);
Student bob = new Student("Bob", 85);

alice.getGrade();      // only Alice's object knows Alice's grade
bob.updateGrade(90);   // only Bob's object can change Bob's grade

Now the data and the operations are together. You cannot accidentally change Alice’s grade when you meant to change Bob’s, because each object protects its own data.

The Four Principles of OOP

OOP is built on four core principles. You do not need to master all four immediately – this page introduces them, and later pages explore each in depth.

1. Encapsulation

Encapsulation means bundling data (attributes) and the methods that operate on that data into a single unit (the class), and restricting direct access to the data. The data is kept private and can only be accessed through public methods.

Think of a bank account: you cannot reach into the vault and take money. You must go through the teller (a method) who checks your identity and balance before processing the transaction.

Why it matters: prevents accidental or unauthorised modification of data; the class controls what values are valid.

2. Inheritance

Inheritance allows a new class to reuse and extend the attributes and methods of an existing class. The new class (child/subclass) inherits everything from the existing class (parent/superclass) and can add or modify behaviour.

Think of a hierarchy: all vehicles share certain properties (speed, fuel), but a car has doors while a motorcycle does not. Instead of rewriting the shared properties, the car and motorcycle classes inherit from a common vehicle class.

Why it matters: promotes code reuse, reduces duplication, creates logical hierarchies.

3. Polymorphism

Polymorphism means different objects can respond to the same method call in different ways. If both a Dog and a Cat have a speak() method, calling speak() on a dog produces “Woof” while calling it on a cat produces “Meow” – same method name, different behaviour.

Why it matters: lets you write code that works with a general type (Animal) without knowing the specific type (Dog, Cat) at compile time.

4. Abstraction

Abstraction means hiding complex implementation details and exposing only what is necessary. When you drive a car, you use the steering wheel and pedals – you do not need to understand the internal combustion engine. The car abstracts away the complexity.

Why it matters: simplifies how other parts of the program interact with an object; reduces cognitive load.

These four principles work together. Encapsulation hides data. Inheritance reuses code. Polymorphism makes code flexible. Abstraction keeps things simple. A well-designed OOP program uses all four.

Key Terminology

These terms appear throughout OOP and in IB exams. Learn them precisely.

Term Definition
Class A blueprint that defines the attributes and methods an object will have
Object A specific instance created from a class
Attribute A variable that belongs to an object (also called a field or instance variable)
Method A function that belongs to an object and can access its attributes
Instantiation The process of creating a new object from a class using the new keyword
Constructor A special method called during instantiation that initialises the object’s attributes
State The current values of all an object’s attributes at a given moment
Behaviour What an object can do – defined by its methods

When to Use OOP

OOP is not always the best approach. It shines when:

  • You are modelling real-world entities (students, accounts, products, vehicles)
  • Multiple objects of the same type need to exist simultaneously
  • Data and operations on that data are closely related
  • The program is large enough that organising code matters

For simple scripts or one-off calculations, procedural code is often simpler and faster to write.


Worked Examples

Example 1: Procedural vs OOP Thinking

Scenario: A vet clinic tracks patients (animals). Each animal has a name, species, age, and vaccination status.

Procedural approach:

String[] names = {"Max", "Bella", "Luna"};
String[] species = {"Dog", "Cat", "Dog"};
int[] ages = {3, 5, 2};
boolean[] vaccinated = {true, false, true};

// To check if Bella is vaccinated, you need to know she is at index 1
// across ALL four arrays. If arrays get out of sync, data is corrupted.

OOP approach:

public class Animal {
    private String name;
    private String species;
    private int age;
    private boolean vaccinated;

    public Animal(String name, String species, int age) {
        this.name = name;
        this.species = species;
        this.age = age;
        this.vaccinated = false;
    }

    public void vaccinate() { this.vaccinated = true; }
    public boolean isVaccinated() { return vaccinated; }
    public String getName() { return name; }
}
Animal bella = new Animal("Bella", "Cat", 5);
bella.vaccinate();
System.out.println(bella.getName() + " vaccinated: " + bella.isVaccinated());

Output:

Bella vaccinated: true

Each animal’s data travels together as one unit. You cannot accidentally give Bella’s vaccination to Max because each object manages its own state.


Example 2: Identifying the Four Principles

For each scenario, identify which OOP principle is being demonstrated:

Scenario Principle Why
A BankAccount class keeps balance private and only allows changes through deposit() and withdraw() Encapsulation Data is hidden; access is controlled through methods
A SavingsAccount class extends BankAccount and adds an addInterest() method Inheritance New class reuses existing class and adds new behaviour
Both Dog.speak() and Cat.speak() exist but produce different output Polymorphism Same method name, different behaviour depending on the object
A Car class exposes drive() and brake() but hides the engine management logic Abstraction Complex internals hidden; only essential interface exposed

Quick Check

Q1. What is the main idea behind object-oriented programming?

Q2. Which OOP principle involves making data private and providing public methods to access it?

Q3. What is instantiation?

Q4. A Car class and a Motorcycle class both share attributes like speed and fuel from a Vehicle class. Which principle is this?

Q5. What does the term "state" mean in OOP?


Fill in the Blanks

Complete the OOP terminology definitions:

A  is a blueprint that defines attributes and methods.

An  is a specific instance created from that blueprint.

 bundles data and methods while restricting direct access.

 allows a new class to reuse and extend an existing class.

 means different objects respond differently to the same method.

The process of creating an object is called .

Practice Exercises

Core

  1. Define in your own words – Write one sentence defining each of the four OOP principles (encapsulation, inheritance, polymorphism, abstraction). For each, give a real-world analogy that is NOT from this page.

  2. Procedural vs OOP – A music streaming app needs to track songs. Each song has a title, artist, duration (seconds), and play count. Describe how you would model this using (a) parallel arrays (procedural) and (b) a Song class (OOP). What problem does the OOP approach avoid?

  3. Terminology matching – Match each term to its definition: class, object, attribute, method, constructor, state, instantiation.

Extension

  1. Identify the principle – For each scenario, name the OOP principle being used and explain why:
    • (a) A Rectangle class hides its width and height and only allows changes through setWidth() with validation
    • (b) A Shape class has a draw() method, and Circle, Square, and Triangle each override it to draw differently
    • (c) An ElectricCar class extends a Car class, inheriting speed and fuel but adding batteryLevel

Challenge

  1. Design thinking – You are designing a system for a school library. The library has books, members, and loans. Each book has a title, author, ISBN, and availability status. Each member has a name, ID, and a list of currently borrowed books. A loan records which member borrowed which book and when.
    • (a) Identify at least 3 classes you would create
    • (b) For each class, list the attributes and methods
    • (c) Identify which OOP principles you would use and where
    • (d) Identify any relationships between classes (has-a or is-a)

Connections

  • Prerequisites: Programming Constructs – understanding method declaration, parameters, and return values is essential before defining class methods
  • Prerequisites: 1D Arrays – arrays of objects follow the same patterns
  • Next: Classes and Objects – putting these concepts into Java code
  • Forward: Encapsulation – the first principle explored in depth

Back to top

© EduCS.me — A resource hub for IB Computer Science

This site uses Just the Docs, a documentation theme for Jekyll.