Four basic concepts of OOP


OOP, also known as object-orientated programming, is a programming paradigm that relies on the concept of classes and objects. In computer science, there are classes based OOP languages such as Javascript, Python, C++, Java, etc. In these languages, the data attributes and functions are defined as a blueprint for instantiating objects. And different objects that are created from the same or different classes can interact with each other in various ways.

OOP in PHP

There are four basic concepts of OOP. Encapsulation is the idea of wrapping data and code together into a single unit. In doing so, we prohibit the access of private data of the current class from an outside class. For example, we are using a data attribute to store the credit card numbers of customers in a class, and by encapsulating this data attribute, we prevent access to the credit card number from the outside code. So the credit card numbers would remain safe inside the class.

Abstraction is the idea of showing only relevant attributes and hiding the necessary information. By applying abstraction to a class, a developer will be able to implement more complex logic on top of the existing class without dealing with the hidden complexity. For example, when we are using an air conditioner, we simply press the buttons to adjust the temperature. We don’t really think the logic behind the buttons and air conditioners, do we? It simply gets the job done.

In OOP, inheritance is the mechanism where you can have one class inherits the attributes and methods of other classes. When you have one class inherited from another, we call the class you inherit from the parent class. The child class will have all the same functionality and properties as the parent and you can also add more code to it. For example, we can have a parent class called animal with two methods “eat” and “sleep”. We can have two child classes that are inherited from the animal class called dog and cat. Both dogs and cats will inherit the “eat”, “makeSound” and “sleep” methods from the animal.

Polymorphism means many forms. In OOP, polymorphism implies that you can access objects of different forms through the same interface. Let’s use the previous example again, both dog and cat classes inherited the “makeSound” method from the animal’s class. We know dogs and cats make different sounds, so we will override the makeSound method in dog and cat classes so that when the makeSound method is called on dog and cat. Dog class will print “bark” and cat class will print “meow”. The same function leads to different behaviors. This is the idea of polymorphism.

Now you have a basic understanding of OOP. It’s time to take advantage of it. I recommend learning an objected orientated programming language and start practicing and seeing how those four basics of OOP work in action.

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *