DEV Community

Jafarscript
Jafarscript

Posted on

The 4 Pillars Of Object Oriented Programming (OOP)

In this article, I will try to explain the 4 pillars of object oriented programming (OOP).Object Oriented Programming allows programmers to think of software development as if they are working with real-life entities.

The four Pillars of OOP are;

Encapsulation

Encapsulation is when each object inside a class maintain a private state. In a way that other object can not access this state directly instead they can only invoke a list of public methods. Also, Encapsulation achieve the concept of data hiding providing security to data by making the variable as private and expose the property to access the private data with which would be public.

Inheritance

The ability of creating a new class from an existing class or ability of one object to acquire some/all properties of another object. For example, a child inherits the traits of his/her parents. It helps to reuse, customize and enhance the existing code. Inheritance allows a class (subclass) to acquire the properties and behavior of another class (super-class).

Polymorphism

Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means "many forms". A subclass can define its own unique behavior and still share the same functionalities or behavior of its parent/super class but a super class can not have the behavior of its subclass. If we had a superclass called Animal that has a method called animalWalk(). The sub-classes of Animal could be Dogs, whales, elephants, and horses. Each of these would have their own iteration of an animal walk (elephant- walk, whale-swim).

Abstraction

Abstraction is an extension of encapsulation. It is the process of showing only essential/necessary features of an entity/object to the outside world and hide the other irrelevant information. Suppose you want to create a work application and you are asked to collect all the information about your users. You might receive the following data from your user: Full name, address, phone number, favorite food, favorite movie, hobbies, tax information, social security number, credit score. This amount of data is great however not all of it is required to create a CV profile. You only need to select the information that is pertinent to your work application from that pool. Data like Full name, Phone number, credit score, and address make sense for a work application. The process of fetching/removing/selecting the user information from a larger pool is referred to as Abstraction.

Top comments (3)

Collapse
 
arvindpdmn profile image
Arvind Padmanabhan

Nice summary of these pillars of OOP. It's also good to know how composition differs from inheritance, which is briefly explained at devopedia.org/object-oriented-prog...

Collapse
 
jafarscript profile image
Jafarscript

Thank you for your comment

Collapse
 
alimikehindem1 profile image
Marusoft

Nice article