Skip to content

OOP Principles

Object-oriented programming is built on four core principles:

  1. Abstraction
  2. Polymorphism
  3. Inheritance
  4. Encapsulation

“A-PIE” for short! (yum yum)

Abstraction is the process of hiding unnecessary implementation details and exposing only the essential features of an object.

In other words, users only need to know what an object does, not how it does it.

Tech tip: Think of how you start a car.

To start the car, you simply turn the key or press the Start button. You don’t need to know how the engine, fuel injection, battery, or transmission work. (That’s for the GCOE people to worry about.)

The inner mechanics are hidden while a simple interface (the key turning) is presented to the driver.

In Java, abstraction is commonly achieved using access modifiers, which determine which parts of a class can be accessed by other classes.

Modifier UML Symbol Accessible From
public + Anywhere in the program
private - Only within the same class
protected # The same class, its subclasses, and classes in the same package
(default/package-private) ~ Classes in the same package only

UML Example 1

In this UML diagram:

  • - brand and - year are private attributes.
  • + start() and + stop() are public methods.

Other classes can call the start() and stop() methods, but they cannot directly access the brand or year attributes because they are marked private.

Equivalent Java Code

class Car {
String brand;
int year;
void start() {
System.out.println("Car started.");
}
void stop() {
System.out.println("Car stopped.");
}
}

Polymorphism means “many forms.” It allows different objects to respond differently to the same method call.

In other words, multiple classes can have a method with the same name, but each class provides its own implementation of that method.

Example: Imagine a teacher tells everyone in the classroom, “Introduce yourself.”

Each student introduces themself but each gives a different response (different name, different intonation, different style) because they are different people.

Likewise, different objects can respond differently to the same method call.

Inheritance allows one class to inherit the attributes and methods of another class.

Instead of writing the same code multiple times, common functionality is placed in a parent class.

Tech tip: Think genetics.

A child inherits characteristics from their parents, such as eye color or height. Likewise, a child class inherits features from its parent class.

In other words, Inheritance allows child classes to reuse the functionality of parent classes.

Note: no UML inheritance examples for now. This will come in the finals

Encapsulation is the process of bundling data and methods into one class while protecting the object’s data from direct access.

Instead of allowing anyone to modify data directly, the class controls access through methods.

Example: Think of an ATM. You can deposit or withdraw money using the buttons on the ATM, but you cannot directly reach inside and change your bank balance.

UML Example 2

class BankAccount {
private double balance = 0;
public void deposit(double amount) {
balance += amount;
}
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.deposit(500);
System.out.println(account.getBalance());
}
}

In this example, the balance variable is marked as private, so it cannot be changed directly from outside the BankAccount class.

Instead, users must interact with the account through the deposit() and getBalance() methods, allowing the class to control how its data is accessed and modified.

Tip: If you get an answer wrong, expand your understanding by consulting the internet on why!

Card 1 / 7
Question

You change a field from public to private and create a method that blocks invalid input before saving. Which OOP principle did you just implement?

Click or press Enter to flip
Answer

Encapsulation. It restricts direct access to an object's fields and forces data modification to go through validation rules.

Click or press Enter to flip back