OOP for Dummies

Adrian Trujillo Duron
5 min readDec 28, 2021

--

Photo by Tom Crew on Unsplash

What is OOP?

Object-Oriented Programming is a fundamental programming paradigm that has become the standard way of coding for most programmers in the world. It is based on the concept of “objects” and tries to mimic real-life objects behavior, storing data and functions related to them. It is used to structure a program into simple, reusable pieces of code blueprints (called classes), that are used to create individual instances of objects.

Classes and Objects are like cookies.

A class is an abstract blueprint used to create more specific objects. On the other hand, an object is an instance derived from a class. This seems a bit repetitive… But let me further explain this concept using cookies. A class can be view like the cookie cutter, every derived object from this blueprint will have the same basic structure. But very object cookie could be further individualized with colored icing, toppings, etc… (I want some cookies now.)

Building Blocks

A class will contain attributes and methods as its building blocks. Attributes refers to the variables that will be contained inside of the object, they will be initialized in the class and defined in the object when a new instance is declared. Classes can also contain functions, called methods, that are only available to objects of that type.

The Four Pillars of OOP

Now we will talk about the four principles of Object-Oriented Programming.

Encapsulation

Photo by Elsa Olofsson on Unsplash

Encapsulation means containing all important information inside an object, and only exposing selected information to the outside world. This principle hides the internal software code implementation inside a class and hides internal data inside of objects. Encapsulation requires defining some fields as private or public.

A private interface refers to methods and properties that are only accesible from other methods within the same class.

A public interface refers to methods and properties that are also accessible from outside of the class.

It adds security to attributes by making them private, in case you really need to access the data, public methods can be implemented to access or modify it on your terms. These methods are commonly referred as getter and setter methods.

Abstraction

Photo by Eliška Motisová on Unsplash

Abstraction means that the user only interacts with selected attributes and methods of an object. It uses simple things to represent complexity, the main goal is to hide complexity details from the user. Take your phone for example, when taking a photo the user (you) only need to press a single button, however many processes are happening inside of your mobile device to process the image and render it on your screen. This information is very complex and would confuse the user so it is better kept hidden.

Abstraction also serves an important security role. By only displaying selected pieces of data and only allowing data to be accessed though classes and methods, we protect the data from exposure.

Inheritance

Photo by Sebastián León Prado on Unsplash

Inheritance is one of the most important aspects of OOP. It allows classes to inherit features of other classes. Put another way, parent classes extend attributes and behaviors to child classes. Inheritance supports reusability.

If basic attributes and behaviors are defined in a parent class, child classes can be created extending the functionality of the parent class, and adding additional attributes and behaviors.

The benefits of inheritance are programs can create a generic parent class, and then create more specific child classes as needed. This simplifies overall programming, because instead of recreating the structure of a class multiple times, child classes automatically gain access to functionalities within their parent class.

Polymorphism

Many forms or a Ditto.

Polymorphism literally means “many forms”. This principle focuses on designing objects to share behaviors. Building from inheritance, polymorphism allows the same method to execute different behaviors in two ways: method overriding and method overloading.

Overriding

This kind of polymorphism takes place when a child class provide a different implementation than its parent class. For example, if you have a Dog class with a bark() method that outputs “Woof!”, you could override this method by making a new implementation of the method in your SiberianHuskyDog child that outputs something like “I’m so cold, woof!” instead. It resolves in runtime.

Overloading

Methods or functions can have the same name, but a different type or number of parameters can be passed into each method call. You can declare different results depending on these parameters. For example, you could have a method called add() that expects no parameters and does a +1 sum. You could have an overloading of this method by declaring it again but expecting x number as parameter, when called it will now do a +x sum.

Conclusion

Photo by Joshua Hoehne on Unsplash

Object Oriented programming requires thinking about the structure of the program and planning at the beginning of coding. Looking at how to break up the requirements into simple, reusable classes that can be used to blueprint instances of objects. Overall, implementing OOP allows for better data structures and reusability, saving time in the long run.

--

--