Object Oriented Programming Concepts

21 November 2022


The four principles of object-oriented programming are encapsulation, abstraction, inheritance, and polymorphism.

Encapsulation:

Encapsulation is achieved when each object keeps its state private, inside a class. Other objects don’t have direct access to this state. Instead, they can only call a list of public functions — called methods.

Example:

 


Abstraction:

Abstraction can be considered as a natural extension of encapsulation. Applying abstraction means that each object should only expose a high-level mechanism for using it. This mechanism should hide internal implementation details. It should only reveal operations relevant for the other objects.

Example:


Inheritance:

It means that you create a (child) class by deriving from another (parent) class. This way, we form a hierarchy.

Example:


Polymorphism:

This typically happens by defining a (parent) interface to be reused. It outlines a bunch of common methods. Then, each child class implements its own version of these methods.

Example: 


 

Ask Us Anything !