OOP: Object Oriented Programming Explained

oop object oriented programming

Object-oriented programming (OOP) is a computer programming model that manages software design around data or objects. In OOP or object-oriented programming, there are four core concepts, these concepts are encapsulation, abstraction, inheritance, and polymorphism. 

Let us look at each of these concepts in detail.

Before object-oriented programming, we had a type of programming that divides a program into a set of functions. We have data stored in many variables and functions that operate on the data. This style of programming is called procedural programming which is a very simple and straightforward programming technique. Often it's what you learn as part of your first programming subject at a university. 

In procedural programming, as your programs grow you will end up with a bunch of functions that are all over the place. You might find yourself copying and pasting lines of code over and over. There is so much interdependence between all these functions that it becomes problematic. object-oriented programming came to solve this problem.

In object-oriented programming, we combine a group of related variables and functions into a unit we call that unit an object. We refer to these variables as properties and functions as methods. To understand with a real example, think of a car. A car is an object with properties such as make, model, and color and methods like start, stop, and move. 

As a real programming example, think of the local storage object in your browser. Every browser has a local storage object that allows you to store data locally. This local storage object has a property like a length that returns the number of objects in the storage. Local storage has methods like setItem and removeItem. 

Encapsulation

Encapsulation is the grouping of related variables and functions into objects. An example of encapsulation in action.

Suppose we have three variables, base salary, overtime, and rate. We also have a function to calculate the wage for an employee. We can couple together all properties and methods into an object which could be named an employee object. 

In an object-oriented way, your functions end up having fewer and fewer parameters as the best functions are those with no parameters. The fewer the number of parameters the easier it is to use and maintain that function so that's encapsulation. 

Abstraction

Abstraction in object-oriented programming is the technique of hiding unnecessary details from users. Let us take an example of a TV as an object, this TV has a complex logic board inside and a few buttons outside that you interact with. Users simply press the buttons to operate the TV and don't care about what happens inside. All that complexity is hidden from users, this is Abstraction in practice. 

We can use the same technique in our objects. We can hide some of the properties and methods from the outside; this gives us a couple of benefits. First is that we will make the interface of those objects simpler.  Using an object with a few properties and methods is easier than an object with several properties and methods. The second benefit is that it helps us reduce the impact of change. 

Let us imagine that tomorrow we change these inner or private methods, these changes will leak to the outside because we don't have any code that touches these methods outside of their containing object or class. We may delete a method or change its parameters but none of these changes will impact the rest of the application's code. With abstraction, the impact of the change could be reduced. 

Inheritance

Inheritance in OOP is a mechanism that allows you to eliminate redundant code. Here is an example, think of HTML elements like text boxes, drop-down lists, checkboxes, and so on. All these elements have a few things in common they should have properties like hidden and innerHTML and methods like click and focus. Instead of redefining all these properties and methods for every type of HTML element, we can define them once in a generic object. Call this generic element an HTML element and have other objects inherit these properties and methods. Inheritance helps us eliminate redundant code. 

Polymorphism

In polymorphism, poly means many; mor means form so polymorphism means many forms. In object-oriented programming, polymorphism is a technique that allows you to get rid of long if-else or switch and case statements. 

In the previous example, we talked about HTML elements. All those objects should have the ability to be rendered on a page but the way each element is rendered is different from the others. If you want to render multiple HTML elements by using object orientation we can implement a render method in each of those objects with different behavior depending on the type of the object you're referencing. We can get rid of this nasty switch and case and use one line of code.

Waqar Kabir

Certified .Net Specialist

Post a Comment

Previous Post Next Post