Principles of Object-oriented programming
Tags: oop, beginners, programming, codenewbie
Published: 2021-10-16 14:27:09

Object-oriented programming has been the most popular programming paradigm for over two decades. It is build on the idea that problems can be broken down in terms of the objects required to solve it.

Let's have a look into the core principles around the object-oriented programming paradigm.

## Building blocks of OOP

### Objects

Objects are entities which have certain attributes and provide some services. 

In programming terms attributes are data held by the object in variables and services are the methods of the object that are accessible from outside.

A Car is an object. Its attributes are its engine, color, model, seats, etc. while its service is to help people travel.


### Class

Class is based on the idea of "type". It provides a template  for defining similar objects and an interface for interacting with the object's services and data. 

In OOP, each object will belong to a class which defines its characteristics and behaviour. Objects, also called as instances of a class, are created, stored and utilized as per the template defined by their class.


## Principles 

### Encapsulation

The object's data and its capabilities are bundled together. And as we already know, they are defined in a class. 

- The class provides an interface for other objects to interact with instances of that class. The point to note about the interface is that it does not need to reveal everything about the object. 
- Some capabilities and data are internal to the object. They are not for the outside world to know or use.

This can be understood in terms of a capsule which is consumed from the outside. It has a composition of medicines inside but they cannot be taken separately.

### Abstraction

Focus on "what" can solve your problem and not on "how" it solves it. More specifically, find the objects which can solve your problem and use their relevant services. 

Objects communicate by calling each other's services to get the job done. At no point does one object need to know the specifics of how the other object is implementing the services. 

Abstraction and Encapsulation work together to ensure that implementation details of an object are hidden from the external world.

For e.g. a Car is an encapsulated unit. It will not tell you what each of its parts are made of and what material was used to create them. However, it will provide you with some abstracted functionalities. 

You can press the accelerator and it will increase your speed without you knowing how it increases the speed or what parts are involved in the process.


### Inheritance

At its core, inheritance is a technique for code reuse. 
 - Inheritance allows a class (*subclass*) to build on top of another class (*superclass*). 
 - The subclass will have all the properties of its superclass and should be able to perform all the services provided by the superclass. 
 - In addition it can define/update a few properties and services of its own.

Sometimes objects required to solve problems will follow a uniform interface but will differ in implementation details. In such cases, inheritance is useful to help in avoiding repetition of code that will remain the same.

A stricter and recommended use case of inheritance is subtyping where we state that if a subclass object "is-a" and "behaves-as-a" superclass object, only then the inheritance is valid.

A Car is a class. Additionally, cars can be of several subtypes. A SUV is also a Car and a LUV is a Car too because they will both have all the attributes of . 

If we were defining SUV and LUV as classes, it would make sense to make them inherit the class of Car.


### Polymorphism

Polymorphism is the ability to behave differently in different situations. 
- This is achieved using a technique called "Late Binding". The code to be executed is determined at runtime. 
- From another perspective, the object or method to be used is determined at runtime. 
- This helps in making the code "decoupled" - not tied strongly with objects of a specific type.

---