What is Object Oriented Programming?

Photo by Kote Puerto on Unsplash

What is Object Oriented Programming?

Introduction to OOP

When developing software, of course, we must first know the paradigm in the programming world. Maybe we have heard of several paradigms such as imperative, object-oriented, procedural, and functional. What is the programming paradigm? A programming paradigm is a style or way we write programs. In this series, we will discuss the object-oriented programming (OOP) paradigm.

Before discussing the OOP paradigm, it is also necessary to know the relationship between the S.O.L.I.D principles (which will be discussed in the upcoming series) in the concept of OOP. Therefore, this S.O.L.I.D principle is a guide when developing a system using the OOP approach. By following these principles, you can create a robust, fragile system that is easy to use and rebuild. In this article you will learn what OOP is and the main pillars of the OOP approach such as encapsulation, abstraction, inheritance, and polymorphism. These will be explained in the next part of the series.

When developing with the OOP approach, you will come across some terms and definitions such as classes, attributes, and methods. It's important to remember that each programming language has its terminology. The meanings of these terms can be almost the same or very different. For example, properties or attributes that have different names but have the same meaning. Programmers typically use the term attribute more commonly related to the mechanism or process that an object performs, and use the term property to describe the characteristics of an object.

Why object-oriented programming? OOP (Object Oriented Programming) is one of the most popular programming paradigms or techniques in software development. The OOP paradigm makes it easy to visualize our code because OOP itself resembles a real-world scenario. When implementing OOP, we combine a set of functions or attributes that have something in common into a unit called an object.

Class is a blueprint that you can develop to create objects. This blueprint is a template that describes the behavior of its objects (in the form of a property or method).

202004081610044e0e18799a32ee7d458f1ca15b9693f6.png

The above visualization provides an overview of the concept of OOP, which is a blueprint for cats, the attributes that cats have, and the skills that cats can perform. In the programming world, especially in the OOP paradigm, we often create many objects of the same type. By creating this design, we can reduce code duplication when creating similar objects.

Class

As explained in the discussion of objects, a class is a blueprint. In this class, we define something which is an attribute or behavior for objects we would like to create.

Cat
- str color+ play()
- int weight+ eat()
- int length+ purring()
- int height+ sleep()

In this Cat class example, the attributes are color, weight, width, and height. As for behavior, it can play, eat, purr, and sleep.

>>> class Cat:
...     def __init__(
...         self,
...         color=None,
...         height=None,
...         length=None,
...         weight=None
...     ):
...         self._color = color
...         self._weight = weight
...         self._length = length
...         self._height = height
... 
...     def play():
...         # play something...
...         pass
... 
...     def eat(food):
...         # eat food...
...         pass
... 
...     def purr():
...         # make a cute sound...
...         pass
... 
...     def sleep():
...         # sleep anywhere...
...         pass

Let's try to create some examples of this Cat class. Basically Instance and object are the same thing. Because when referring to an object, we know that an object is an instance of a class that we create from a blueprint, or in other words, an object is the result of the instantiation of a blueprint. Objects that are created from a class will have access to the entire attributes of that class. Is it still confusing? Let's take a look at an example of creating an instance in the code snippet below.

>>> def main():
...     persian = Cat()
...     persian.color = "White"
...     persian.weight = 2.0
...     persian.length = 46.0
...     persian.height = 24.0
...     anggora = Cat("Gray", 25.0, 41.0, 2.4)
...     bengal = Cat("Coklat", 22.0, 39.0, 2.3)
... 
... main()

Pretty easy, right? We can repeat this code sample with different arguments to create other instances.

Property

Properties (also called attributes) are data contained in an object or class. An object certainly has properties that represent the characteristics of that particular object. Each object can have different properties. In the previous example, the Cat class has some properties which are color, weight, length, and height.

>>> class Cat:
...     def __init__(
...         self,
...         color: str = None,
...         height: int = None,
...         length: int = None,
...         weight: int = None,
...     ):
...         self._color = color
...         self._weight = weight
...         self._length = length
...         self._height = height

In the above code snippet, we can see the properties of a class. Generally, each property has a basic structure such as a modifier, data type, constant name and value.

Methods

A method is a function that is associated with a class or an object. When we call a method, a mini-program is executed. The function itself can be interpreted as a simple way to organize our programs. For example, animals have several behaviors or methods that they can perform such as eating, walking, or communicating with other animals.

>>> class Cat:
...     # ...
... 
...     def purr():
...         print("Meow...")
... 
...     def eat(self, food_weight=None):
...         if food_weight >= 0:
...             self.weight += food_weight
...         self.weight += 1

Note that the .purr() method can make this object displays the text "Meow..." or, in a real-world scenario, make the cat purr. The .eat() method can also change the value of one of its properties, which is .weight in this example when the cat eats. So, a method can be a way to alter a property associated with an object.

At this point, we learned much about the point of "what" Object Oriented Programming is. We will discuss more advanced topics in the next part of the series. Stay tuned and don't forget to subscribe to my newsletter.