DEV Community

Cover image for The four principles of object-oriented programming
Aniket Vaishnav
Aniket Vaishnav

Posted on

The four principles of object-oriented programming

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

1. Encapsulation

Encapsulation is the mechanism of binding the data
together and hiding it from the outside world. Encapsulation is achieved
when each object keeps its state private so that other objects don’t have
direct access to its state. Instead, they can access this state only through a
set of public functions.

2. Abstraction

Abstraction can be thought of as the natural extension of
encapsulation. It means hiding all but the relevant data about an object
in order to reduce the complexity of the system. In a large system, objects
talk to each other, which makes it difficult to maintain a large code base;
abstraction helps by hiding internal implementation details of objects
and only revealing operations that are relevant to other objects.

Inheritance

Inheritance is the mechanism of creating new classes from existing ones.

Polymorphism

Polymorphism (from Greek, meaning “many forms”)
is the ability of an object to take different forms and thus, depending
upon the context, to respond to the same message in different ways. Take
the example of a chess game; a chess piece can take many forms, like
bishop, castle, or knight and all these pieces will respond differently to
the ‘move’ message.

Top comments (0)