No history yet

Advanced OOP Principles

Beyond Procedures

In programming, there are different ways to organize your code. One older approach is Procedure-Oriented Programming (POP). Think of it like a recipe. You have a list of steps (procedures or functions) to follow from top to bottom. The main focus is on the actions, and the data is often just passed around from one function to another.

Object-Oriented Programming (OOP), on the other hand, flips this idea. Instead of focusing on the steps, it focuses on the things involved. It bundles data and the functions that operate on that data into single units called objects. This approach models the real world, making code more intuitive, reusable, and easier to manage as programs grow more complex.

FeatureProcedure-Oriented Programming (POP)Object-Oriented Programming (OOP)
FocusOn functions and procedures (the 'how')On objects and data (the 'what')
ApproachTop-down designBottom-up design
Data SecurityData is less secure; moves freelyData is hidden and protected (Encapsulation)
ModelingPoor real-world modelingExcellent real-world modeling
ReusabilityLow; functions are specificHigh; objects can be reused (Inheritance)

The Object Factory

So, how do we create these objects? We use a class. A class is not an object itself; it's the blueprint or template for creating objects. Think of a car factory. The engineers create a detailed blueprint for a new car model. This blueprint specifies the properties (like color, engine size) and behaviors (like startEngine(), accelerate()).

The blueprint is the class. Every car that rolls off the assembly line, built according to that blueprint, is an object. Each car has its own specific properties (one is red, another is blue) but shares the same fundamental behaviors defined in the blueprint. For this reason, a class is often called an 'object factory'.

// The blueprint for creating Car objects
class Car {
    // Properties (data)
    String color;
    String model;

    // Behaviors (methods)
    void startEngine() {
        System.out.println("Engine has started.");
    }

    void accelerate() {
        System.out.println("The car is moving.");
    }
}

A class acts as a user-defined data type. Just as you can create a variable of type int or String, you can create an object of type Car.

Once the class is defined, you can produce as many Car objects as you need:

Car myCar = new Car(); Car friendsCar = new Car();

Here, myCar and friendsCar are two distinct objects, each an instance of the Car class.

Protecting and Simplifying

Two core principles of OOP, encapsulation and abstraction, are all about managing complexity. They sound similar, but they solve different problems.

The four principles of object-oriented programming are encapsulation, abstraction, inheritance, and polymorphism.

Encapsulation is about bundling data (variables) and the methods that operate on that data into a single unit: the class. It also involves protecting that data from outside interference, a concept known as data hiding. In Java, we use access modifiers like private to prevent direct access to a class's variables. To interact with the data, you must use the public methods the class provides (often called getters and setters).

class Smartphone {
    // Data is hidden from the outside world
    private int batteryLevel;

    // Public method to get the data
    public int getBatteryLevel() {
        return batteryLevel;
    }

    // Public method to set the data
    public void charge(int amount) {
        if (amount > 0) {
            this.batteryLevel += amount;
            if (this.batteryLevel > 100) {
                this.batteryLevel = 100;
            }
        }
    }
}

In the Smartphone class, you can't just write myPhone.batteryLevel = 500;. You have to use the charge() method, which contains logic to ensure the battery level doesn't go above 100. This is the power of encapsulation.

Abstraction, on the other hand, is about hiding implementation details and showing only the essential features. When you use a smartphone, you tap an icon to open an app. You know what will happen, but you don't need to know how the operating system loads the app into memory. The complexity is abstracted away.

In short: Encapsulation hides data within an object. Abstraction hides the complex 'how' behind a simple 'what'.

One Name, Many Forms

The final two pillars of OOP are Inheritance and Polymorphism. They work together to create flexible and reusable code.

Inheritance is the mechanism where a new class (a subclass or child class) acquires the properties and methods of an existing class (a superclass or parent class). This promotes code reusability. Why write the same code twice? If a Motorcycle and a Car both have a model and can startEngine(), you can define these common features in a parent Vehicle class and have both Motorcycle and Car inherit from it.

Lesson image

Polymorphism, which means "many forms," allows us to perform a single action in different ways. In Java, one common way to achieve this is through method overloading. This is when two or more methods within the same class share the same name but have different parameters (either a different number of arguments, or different types of arguments). The compiler knows which one to call based on the arguments you provide.

class Calculator {
    // Method to add two integers
    int add(int a, int b) {
        return a + b;
    }

    // Same method name, but with three integers
    int add(int a, int b, int c) {
        return a + b + c;
    }

    // Same method name, but with double types
    double add(double a, double b) {
        return a + b;
    }
}

// Usage:
Calculator calc = new Calculator();
calc.add(5, 10);      // Calls the first method
calc.add(5, 10, 15);  // Calls the second method
calc.add(3.5, 2.7);   // Calls the third method

Method overloading makes your code cleaner. Instead of creating methods like addTwoNumbers(), addThreeNumbers(), and addTwoDoubles(), you can just use add(). The context determines the behavior.

Quiz Questions 1/6

What is the primary difference in focus between Procedure-Oriented Programming (POP) and Object-Oriented Programming (OOP)?

Quiz Questions 2/6

In the analogy of a car factory, the detailed engineering blueprint for a car model represents a class, while an actual car built from that blueprint represents an object.

These four principles are the foundation of object-oriented design. Mastering them allows you to write code that is not only functional but also clean, modular, and easy to maintain.