Principles of OOP

natasha selvidge
2 min readAug 11, 2021
Photo by Fotis Fotopoulos on Unsplash

Object-oriented programming is one of the most common ways of writing code and, one of the best-known paradigms out there. The moment you start learning to code anywhere OOP is usually covered.

The four basic principles of object-oriented programming:

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

Encapsulation — the process of bundling data and operations done on the data together into a single unit. This is mechanism of hiding data implementation by restricting access. Usually, we have 3 main access specifiers:

  • public — members are accessible from outside the class
  • protected-members cannot be accessed from outside the class, but can be accessed in inherited class.
  • private-members cannot be accessed from outside the class.

Abstraction — used to hide implementation details. It hides and handles complex logic from the user. Users can further implement more complex logic on top of abstraction that is already provided. One example of abstraction is when we interact with our phones. All we do is press a button and we make a call. In reality a lot of things happen under the hood that we maybe don’t know or don’t need to know.

Inheritance — a mechanism in which one object acquires all the states and behaviors of a parent object. It is used to derive a new type from an existing type, thereby establishing a parent-child relationship. In programming we use an inheritance in the following example: a Vehicle which would be the parent class of the Car and Bus classes. Both Car and Bus have the properties such as number of wheels, engine power, color and methods such as drive, start and stop.

Polymorphism — means “many shapes”. Programmers can change the way that something works by changing the way it is done or just some parts of it. Polymorphism can be static and dynamic. Static is achieved by method overloading, and dynamic by method overriding. If a class has multiple methods that have the same name but different parameters, this is known as method overloading. If subclass has the same method as declared in the super class, this is known as method overriding.

--

--