What is OOP?

Object-oriented programming is an approach to software development in which developers model the code they write on real-world things.

Those things, or objects, are given properties, behaviors, and actions they can perform. An object-oriented system comes alive when these objects programmatically interact with each other by calling each other’s methods, changing each other’s properties, and triggering each other’s actions.

A car modeled as an object with state and methods
When using object-oriented programming, a car would be modeled as a class with state, represented by properties, and behaviors, represented by methods.

How do objects use properties and methods?

Most object-oriented programming languages, such as Java, C++ and Python:

  • use the concept of a class to represent an object;
  • put variables inside a class to represent an object’s properties or state; and
  • define methods to describe an object’s behaviors.

Can you give me an example of object-oriented programming?

In the following example, a Java class defines an object that has two properties. The object has a color and a runningEngine, which can be either true or false. That’s the object’s state.

public class Car
{
  int speed;
  int fuelPercentage;
  String color = "red";
  boolean runningEngine;

  public void drive() { speed = 65;            }
  public void start() { runningEngine = true;  }
  public void stop()  { runningEngine = false; }
  public void refuel(){ fuelPercentage = 100;  }

}

Notice how the car has methods like stop() and start().

The methods are the things the object can do, and when other objects call these methods, the state of the object, namely whether the engine is running, can change.

That’s the essence of object-oriented programming. Objects interact with other objects through well-defined methods or interfaces, and those method calls have the power to change another object’s state.

What are the benefits of object-oriented programming?

The key benefit of object-oriented analysis and design (OOA&D) is that it simplifies software development by allowing programmers and architects to break down complicated systems into real-world entities that are conceptually easy to understand.

Furthermore, by breaking systems down into small, reusable objects, giant programs become:

  • more modular;
  • easier to maintain;
  • more secure;
  • less expensive to build;
  • increasingly scalable; and
  • highly flexible and adaptable.
Object state and methods in an OOP system
Objects in OOP-based systems provide a public interface that other objects can use to invoke methods and change an object's state.

How does object-oriented programming work?

Languages such as Java, C++, and Rust help developers do object-oriented programming by providing language features that allow developers to create objects that implement the most important OOP principles, including:

  • Inheritance: The idea that one object can be a specialized subtype of another object.
  • Encapsulation: The ability of an object to protect internal data from other objects.
  • Abstraction: The idea that many different objects can abstractly share common properties and behaviors.
  • Polymorphism: The idea that an object can take on many different forms throughout its lifespan.
  • Association and composition: Ways to model the concept that one object may be an essential part of another object.
The SOLID principles of object-oriented programming
How do you design good object-oriented systems? Follow Uncle Bob's SOLID principles.

What are OOP’s SOLID principles?

There are many approaches to designing objects, but Uncle Bob Martin’s SOLID principles have been a guiding light for object-oriented architects for over 20 years.

OOP’s SOLID principles dictate that a well-built, object-oriented component:

  • S: Has a single responsibility.
  • O: Should be open for extension but closed for modification.
  • L: Obeys the Liskov Substitution Principle for subclass substitutions.
  • I: Uses small, well-defined, and highly focused interfaces to define behaviors.
  • D: Implements the Dependency Inversion Principle (DIP), where abstractions are preferred over concrete implementations.

Object-oriented programming explained

That’s what object-oriented programming is all about:

  • You model complex systems based on real-world things.
  • You give objects state and behaviors.
  • Things happen when objects interact.

To implement an object-based system, languages such as Java, Python, C#, and Rust provide ways to implement inheritance, composition, abstraction, and polymorphism.

UML model of an object-oriented system
UML helps developers model object-oriented systems. Here is the UML for an object-oriented duck designed according to SOLID OOP principles.

Pay attention to Uncle Bob!

And if you follow Uncle Bob’s SOLID principles of object-oriented design, your systems will be flexible, scalable, adaptable, and easy to maintain.

Object-oriented programming is a powerful approach to software development, which is why it has been a dominant force in the programming world for over 30 years.