Advertisment

OOPS Explained

author-image
PCQ Bureau
New Update

Let us start this article with a statement that many of you will agree with–programming is an art. And like any other art form, there are many schools of thought on programming, each with its own merits and demerits. Programmers follow their chosen line of art–their programming system–as religiously as those in other art forms.

Advertisment

Traditional programming, of the kind seen in languages like BASIC, Pascal or C, follows what is known as the top-down approach. Here, the problem is broken down into steps and sub-steps and the programming logic follows this same step-by-step approach. This method is still very popular today.

The other very popular and important programming methodology is called Object Oriented Programming (OOP). We’ll take a deeper look at this here. However, although many languages claim to be object-oriented (OO), very few actually are. C++ and even Java are not fully OO. SmallTalk and the new C# are full OO languages. 

One of the most important things to know about OOP is that prior knowledge of traditional methods is not required, and can even be a hindrance in understanding and programming in OOP. OOP is such a different way of thinking in terms of programming that sticking to traditional methods can be a disadvantage. 

Advertisment

Objects, classes, and instances

Simply speaking, in OOP, ‘everything is an object’. The statement may sound weird to you if you’re a non-OOP programmer. But consider this statement more deeply. In real life, do you normally think in terms of function calls, procedures, structures, etc? Or do you think more naturally in terms of ‘entities’ or objects, like a person, a car or a book? Each object has some ‘attributes’ stored within, and to retrieve these, you usually ‘query’ the object itself. For example, to know a person, you ask him his name. To find out the price of a book, you pick it up and look at the print on it. Each of these objects can provide you information pertaining to itself. 

Now look at it in terms of programming. Suppose you had an object called ‘Employee’. All employees will have similar attributes and you’ll be able to find out details about any employee simply by querying the particular employee object, also called an ‘instance’ of the employee object. For example, an employee is sure to have a name, age, address, department, designation, and salary. You can create instances of the base object for each employee you have, and simply change the information stored for each of them. 

Advertisment

These objects can also contain ‘methods’, which are functions that are specific for the object and can access even ‘hidden’ attributes. Methods can also be hidden or visible (or private and public, as they are better known) and other objects can use public methods to query the object for information. Any attribute manipulation can happen only through the methods that have been provided as public. 

In programming an object, most languages let you create a ‘blueprint’ of the object in what is known as a ‘class’. The class contains the attributes and the methods that define the object. To use a class, you have to create an instance of the object and initialize the required attributes. 

Now, let’s move on to the three most important concepts of OOP. 

Advertisment

Encapsulation

OOP gives you certain advantages over other ways of programming. The main problem with the traditional approach is that everyone involved needs to know everything about everything in the project. Every structure, function, and calculation must be kept in mind while programming. So in the above example, if the employee object had a method which returned his salary minus tax cuts, and later you wanted to change the calculation of tax, all you need to change is the code in the employee method itself. Other programmers need not even know what the changes you made, as long as they still get their result. This is called ‘black-box’ programming, in reference to the cockpit flight data recorder. The pilot doesn’t know or care what the black box does as long as it functions correctly. In standard programming, everyone would have to be informed what the changes were so that their part of the code doesn’t break. 

Inheritance

Advertisment

This is yet another area where OOP scores over traditional methods. Objects can ‘derive’ or be ‘derived by’ other objects and hence inherit the derived objects’ properties. That is, if object A is derived by object B, the latter inherits all the methods and attributes of the former. In our employee example, say, the object employee has been created and been in use for some time. Now the company wishes to record details about contract employees and consultants too. Although the basic structure remains the same as that of an employee, each of these have certain attributes and actions that are specific to themselves. 

So, instead of creating complete objects from scratch for each of these, you can derive the employee object into both the contract and the consultant objects and create the attributes and methods specific for each. 

Another advantage of deriving objects is that you can group all derived objects by their ‘base class’. That is, you can create a data structure like an array based on the base class (the employee) and have individual elements in it point to all the different objects like the contract ones and consultants too.

Advertisment

Polymorphism

Inheritance can be further extended by using a functionality called polymorphism. If there are methods that are to have a common name but function differently, say different tax rules and calculations, these can be overridden by having identically named methods that perform differently in the new objects. This allows for great deal of code reuse without understanding how the earlier code was developed. 

Finalé

By now you’d have begun appreciating how important and useful OOP can be, both in terms of conceptualizing a problem and in coding. OOP can help you manage systems’ study and implementation and project handling much better if its diktats are followed. 

Vinod Unny

Advertisment