DEV Community

Cover image for OOP: Object Oriented Programming
Joseph Maurer
Joseph Maurer

Posted on

OOP: Object Oriented Programming

Object Oriented Programming (OOP) is a programming paradigm that is the cornerstone of modern software development. While OOP isn’t the only programming paradigm that exists, I would argue it is the most important to have a firm foundation of, and when used correctly, can lead to stronger and more robust software. But what is OOP and how can you make sure to understand it well? Let’s explore.

High Level

At a high level, in OOP there are 3 main pillars to understand for getting OOP down:

  • Encapsulation with classes
  • Inheritance
  • Polymorphism (the most important by far)

Encapsulation

Encapsulation is the ability to enclose data that describes the object and the functions used to manipulate that data into one module container.

In OOP these are typically called classes and are at the center of the paradigm. The resulting variable when the variable is declared is called an object. An important part is abstraction so that only necessary features are exposed.

  • Private/Internal: Methods and properties are accessible from other methods of the same class. Anything outside of the class shouldn’t be able to access these members/methods. This is typically denoted by Private or Protected.
  • Public/External: Methods and properties are accessible from outside the class.

An example would be if you were programming a card game. In your card dec class, would you want someone else to modify the number of cards in the deck? Probably not.

Inheritance

Inheritance is the ability to take an existing class and extend its functionality to form another class.

Why is this important? Because it supports reusability with code reuse. This is because the new class has access to all of the parent functionality. The child class just needs to add new attributes and methods that are specific to its needs.

  • Generalization is the relation such that the subclass is a variation of the parent. I.e. a pickup truck “is-a” automobile.
  • Specialization is the relationship such that the class “has-a” subclass. I.e. an automobile “has-a” engine.

image

Polymorphism

Polymorphism is the ability of similar objects to respond differently to the same message.

The final pillar of OOP is closely related to inheritance and yet is arguably far more important. The prefix poly means many; morph means form. Polymorphism then refers to the ability of a single type or class to take many forms.

How is this done? With method overriding:

image

In the example above, you can see that both the electric car and the pickup truck inherit from Automobile and both override the Start behavior since both have vastly different needs. An electric car doesn’t need to start an engine while a pickup truck would (assuming it’s of the gas variety).

Key Takeaways

  1. Well designed objects manipulate the data or state of an object by passing messages through the object’s interface.
    1. Controlling an object through its interface guarantees it behaves in a safe manner and that the internal state remains valid.
  2. The ability to model the real world is an important advantage of OOP.
  3. Objects must be self-contained so that they can stand on their own.

OOP isn’t the only programming paradigm out there. Can you name any others? Follow me on twitter for more programming posts!

Top comments (0)