DEV Community

WestonDease
WestonDease

Posted on

OOP Part 1

Programming is a field where many elements come together to form complex interactions. Some problems will need to manage separate yet similar objects for the sake of handling data, performing operations, or to even simply the task. If you are not familiar with the concept of object oriented programming or its properties and uses, this guide is a great place to start.

Object orient programming (OOP) is a common and powerful logical process where variables and operations are grouped into objects based on their uses towards some end goal. There are four main pillars of OOP: abstraction, encapsulation, inheritance, and polymorphism. We will take a look at each of these elements to give you a clear sense of what these words mean and demonstrate their use.

  1. Abstraction

Abstraction is a topic that fascinates me because ultimately (not to get too philosophical) human understanding of everything is heavily rooted in this concept. When a system has many repeating process that that layer over each other and those processes display some emergent property, the human mind uses abstraction to simplify and understand it. This is done so that a system can be understood without thinking about how each individual part of the system works and more complex logic can be built on top of it. For example, you generally understand how atoms work. They're made of particles like electrons, neutrons, and protons. They come together to form molecules which are just different atoms bonded together. Then molecules come together to form compounds and mixtures. These can interact to form proteins. Proteins interact to form cell organelles. Those interact to form cells. Cells form tissues. Tissues form organs. Organs form organisms. Organisms form ecosystems. Ecosystems form the environment. And the process could go on and on seemingly endlessly as we have yet to uncover the true extent of complexity in the universe. Even before atoms there are quarks and it is still unknown how far the rabbit hole goes. But enough about physics, where here to program.

So how does this apply to programming? When we approach a problem we don't need to know how every element works to understand the whole system, especially when we are only working on some narrow slice of it. When we are making "hello world" we don't need to understand the way the compiler works, or machine language, or really what a computer is. If we were writing these instructions on a piece of paper and sliding it under the door of some "Programming Chinese Box"
our understanding at the core level would be the same. So if we look at Node.js, Express, or whatever framework/library you are using to program, you don't need to know how Node fetches tags and classes from an HTML doc. The implementation of the library is irrelevant to your aims. It's the problem that you implement the library in that matters.

  1. Encapsulation

Data is key. There has been more data created in the past two years than in the rest of human history combined. Security of data is an increasingly difficult problem as the years progress due to hackers becoming more voracious and aggressive and the data their after becoming more precious to its holders. But we're not trying to fix that, we just want people to not mess up our logic.

With encapsulation, we can choose how much information is given to whoever is utilizing our programs. Setting things to public or private is a simple way to achieve this, however this doesn't quite get it all. Some languages like JavaScript have no public or private attributes. And even the languages that do aren't exactly flexible enough for our purposes. If, when defining our objects and functions, we include them in a return statement with the attributes we want to be public, we can define precisely what and how things can be accessed.

That's all for now but comeback next time when we go deep into inheritance and polymorphism.

Top comments (0)