DEV Community

Bharadwaj
Bharadwaj

Posted on

Object Oriented Programming (OOP)

Let us look into Object Oriented Programming Concepts in a brief manner.

Programs in general contain data and code.

Process / Procedure Oriented model is a concept where the code is written in a way that it 'acts on the data'. For example, C programming language is written as series of procedures and functions.

OOP is a programming approach / style where the code is implemented 'around the data'. In other words, the data controls access to the code.

The data is referred to as 'Objects'.

What is an Object is this context?

Object is akin to a real world entity that has A) Properties and B) Actions

For example, consider Human being as object.

The object 'Human' has
Properties: Name, Gender, Age
Actions: Walk, Talk, Eat

Let us consider another example, 'Car' as an object.

The object 'Car' has
Properties: model, color, transmission type
Actions: Start, Stop, Accelerate

Properties of an object can also be referred as 'State' (or) 'Attributes'

Actions of an object can also be referred as 'Methods' (or) 'Functions'

What are the principles of OOP?

Let us understand the meaning of these words in simple English and then correlate to the programming approach.

Abstraction:

In our everyday lives, we manage and deal complexity through abstraction. For instance, we manage our home television features through a remote control device; however we may not bothered about the device internal design and ignore how it works. The remote control has specific functions and we understand how to use it through the buttons - TV Power ON / OFF, Volume Increase / Decrease, Channel Increase / Decrease, Mute etc.

In other words, the implementation details are hidden from the user.

This concept can be applied for writing software code where objects are defined as required for the business case. These objects have unique properties and behavior. The code is written in a way to tell the object what to do.

Encapsulation:

Think of a capsule that holds together medicine in a tube (or) container that doesn't expose the what's inside it to the outside world.

Encapsulation in OOP is a similar mechanism that binds data and code inside a wrapper not allowing other code to access. This is required to protect data from unauthorized access and modification.

The methods and variables defined within a class will be marked public or private to control the access. We'll review these topics in detail in upcoming weeks.

Inheritance:

Inheritance means an object acquiring the properties from another object. Imagine this concept like a child inherits the wealth from ancestors and still creates more wealth through his/her own talent. In programming, inheritance avoids rewriting same code for the related objects and helps in reusability.

Polymorphism:

Poly + Morph = multiple forms. Imagine a person known to you. He/She is likely to be parent at home, employee at an organization, social worker for community services etc. So, the person is same; yet depending on the territory their actions / behavior differs. Similar concept in OOP where the methods defined in one class shall exhibit different behavior based on parameters defined with it.

Top comments (0)