Unit 2 · Introduction to Object-Oriented Programming

2.2.1The four pillars and a worked example

The four pillars of OOP (preview)

Object-Oriented ProgrammingEncapsulationhide the dataAbstractionshow essentialsInheritancereuse a classPolymorphismone call, many formsClasses and objects
The four pillars of object-oriented programming rest on classes and objects

Unit 3 teaches each in detail.

Each pillar in one paragraph

Encapsulation puts the data and the methods that work on it together in one class, and hides the data behind those methods. A BankAccount keeps its balance private; you can only change it by calling deposit() or withdraw(), which check every change. Learn more in 3.4.

Abstraction means showing only what the user of an object needs and hiding the details. When you call account.withdraw(500) you do not need to know how the bank records the transaction. You only need to know what the method does. Learn more in 3.7 and 3.8.

Inheritance lets a new class take on the attributes and methods of an existing class and add its own. A SavingsAccount is a BankAccount with interest added, so it reuses all the account code instead of copying it. Learn more in 3.5.

Polymorphism ("many forms") means one method call can behave differently depending on the object. Calling calculateInterest() on a savings account and on a current account runs a different version for each, but the calling code is the same. Learn more in 3.6.

How the pillars work together

The pillars are not separate tricks: they support each other. Encapsulation makes each class safe and self-contained. Abstraction decides what each class shows to the outside. Inheritance builds families of related classes. Polymorphism lets code work with the whole family through the general type. A well-designed program uses all four at once.

Worked example: Student marks system

Procedural thinking: arrays names[] and marks[]; functions addStudent(), printReport(), calculateAverage(). Adding "grade" means changing arrays and every function.

OOP thinking: a Student class with name, marks, and methods getGrade(), printReport(). Adding grade logic changes only the Student class.

Here is a sketch of the OOP version. Everything about a student is in one place:

Java
class Student {
    private String name;
    private int marks;

    Student(String name, int marks) {
        this.name = name;
        this.marks = marks;
    }

    String getGrade() {
        if (marks >= 75) return "A";
        if (marks >= 65) return "B";
        if (marks >= 50) return "C";
        return "F";
    }

    void printReport() {
        System.out.println(name + ": " + marks + " (" + getGrade() + ")");
    }
}

If the grade rules change, you edit getGrade() and nothing else. In the procedural version, the arrays names[] and marks[] must be kept in step by hand: if one function sorts the marks but forgets to sort the names, every report is wrong.

Advantages and disadvantages of OOP

AdvantagesDisadvantages
Models real-world things naturally, so the design is easier to discussTakes more planning before you start coding
Data is protected by encapsulationSmall programs can become longer than necessary
Code is reused through inheritance and compositionPoorly designed class hierarchies are hard to change
Changes usually stay inside one classThere are more new ideas to learn at the start
Teams can work on different classes at the same timeCan use a little more memory and run a little slower

Remember

  • Encapsulation hides data, abstraction hides complexity, inheritance reuses code, polymorphism lets one call take many forms.
  • In OOP, data and the rules about that data live together in one class.