What is Object-Oriented Programming? (OOP)

March 3, 2023

☕️ Support Us
Your support will help us to continue to provide quality content.👉 Buy Me a Coffee

What is Object-Oriented Programming (OOP)?

Object-oriented programming (OOP) is a programming methodology that considers things (called objects) in a software system as entities with states and behaviors and organizes them into an interactive system. This programming method emphasizes concepts such as encapsulation, inheritance, and polymorphism, making code more modular, maintainable, and extensible. OOP has become one of the main paradigms of modern software development and is widely used in many areas, including desktop applications, web applications, mobile applications, game development, and more.

The Three Characteristics

The three characteristics of OOP are encapsulation, inheritance, and polymorphism. Their definitions are as follows, and they are demonstrated using the example of a car class:

Encapsulation**

Encapsulate an object's state and behavior in a single unit and provide limited external interfaces to access the object, thereby protecting the object's internal state from direct access or modification.

public class Car {
    private String make;
    private String model;
    private int year;

    public Car(String make, String model, int year) {
        this.make = make;
        this.model = model;
        this.year = year;
    }

    public void drive() {
        System.out.println("The " + this.make + " " + this.model + " is now being driven");
    }
}

Inheritance

Create a new object that inherits the properties and methods of one or more existing objects, enabling the new object to reuse the existing object's code while also extending or modifying its functionality.

Polymorphism

Allows an object to exhibit different behaviors depending on the context, meaning that an object can be used as multiple different objects, thus improving program flexibility and extensibility. Polymorphism is usually achieved through overloading and overriding.

public class SportsCar extends Car {
    public SportsCar(String make, String model, int year) {
        super(make, model, year);
    }

    @Override
    public void drive() {
        System.out.println("The " + this.make + " " + this.model + " is now being driven fast");
    }

    public void drive(int speed) {
        System.out.println("The " + this.make + " " + this.model + " is now being driven at " + speed + " mph");
    }
}
  • Overloading: The SportsCar subclass defines another drive method, but this time we add an additional speed parameter. This is an example of method overloading. When we call the drive method with the speed parameter, Java automatically recognizes which method we are calling and decides which method to use based on the number and types of parameters.
  • Overriding: SportsCar inherits from the Car superclass and overrides the drive method in the superclass. The @Override annotation is used on the drive method in the SportsCar subclass, which is an example of method overriding. When we create an object of the SportsCar class, the drive method of the subclass is called instead of the drive method of the superclass.

Finally, let's test the results:

int main() {
    Car myCar("Toyota", "Camry", 2021);
    myCar.drive(); // Output: The Toyota Camry is now being driven

    SportsCar mySportsCar("Porsche", "911", 2021);
    mySportsCar.drive(); // Output: The Porsche 911 is now being driven fast
    mySportsCar.drive(120); // Output: The Porsche 911 is now being driven at 120 mph
    return 0;
}
☕️ Support Us
Your support will help us to continue to provide quality content.👉 Buy Me a Coffee