What is Object-Oriented Programming (OOP)?

Posted 3 months, 3 weeks ago | Originally written on 30 May 2013

Before I tell you about OOP I have to tell you a few other things that you will understand very easily. Then when I tell you about OOP you will just get it.

A programmer is someone who write instructions that direct a computer to perform a desired task. The programmer does this using a programming language. The programming language specifies two things: commands and data structures. Commands are explicit instructions that execute an operation such as adding, multiplying or copying the contents of one memory location to another. Commands act on data structures, which store the data that is acted upon.

There are many types of data structures. Some are native to the programming language. This means that the programmer does not have to build them from scratch. There are data structures that handle numbers or characters. These can be further divided into data structures for integers, real numbers, single characters, sequences of characters. Unsurprisingly, each command is specified to work on a single data structure. The result of running a command may be a different data structure. For example, if the programmer wants to add two numbers he may use integer data structures then apply the ADD command. The result with be another integer.

As the programmer continues to carry out his work, he will eventually realise that there are tasks whose results are specified by a series of values. For example, if the program he is writing is a data logger, the value at different instances in time will be required. If there are ten time instances then ten values are need. He will thus need a data structure to hold all ten values at once while all values are of the same type such a integers or real numbers. Such a data structure is referred to as a compound data structure.

Compound data structures need not be confined to values of a single type; they could consist of a collection of types. Now the data structures begin to become more complex and new possibilities begin to emerge. The programmer can now use these complex data structures to hold values that represent a single entity. For example, a complex data structure may now be defined to represent a real-world object such as a car record. This data structure may hold information about the name of the car (a sequence of characters, called a string), the date of manufacture (a date value), the dimensions of the car (a sequence of real numbers), whether it is insured (a true-false value, called a boolean), the number of previous owners (an integer value) or any other detail that the programmer chooses. At the same time, the programmer can specify commands that treat the car as a whole, for example, a command for comparing two cars to each other.

Evidently, the ability to define useful data structures is very powerful. The programmer can now devote his attention to two tasks: constructing relevant data structures and defining commands to handle them. But can he do better?

Suppose the programmer chose to include within the specification of each data structure the commands relevant to that data structure. While he will still carry out the two tasks mentioned above he will isolate the data structure so that all commands he defines will only apply to itself.

A data structure that specifies the set of commands that act on it as part of its structural definition is called a class. Whenever the class is used to create an actual datum, technically referred to as an instance, the result is an object. Classes are used to make objects. Programming around objects (instances of classes) is called object-oriented programming.

Designing OO programs integrates the utility of relevant data structures and the commands that act on them. This gives the workflow a natural feel because the programmer can now concentrate on meaningful interactions between objects making his code vastly easier to follow and improve. OOP also provides many facets that make it easy to build complex programs. Learning how to write OO programs is a vital asset for any data analyst and the investment is well worth making. Initially, learners may be overwhelmed by the terminology but with time they get used to it. It is advisable to learn OOP on actual problems instead of merely mastering the terminology. Each problem afford numerous angles by which the programmer may approach it and it takes years of diligent practice to master the art of developing potent objects.