What on earth is Object Oriented Programming?

If you are in some way related to the field of computer science, it’s highly unlikely that you haven’t heard of the phrase ‘object oriented programming’. For the beginners, it’s a hard-to-perceive concept. At least, for me, it was. So what on earth is this object oriented programming? Let’s take a look.

One important thing to note before reading this article. Computer programming doesn’t occur in two levels (programmer and user) as you may think. There may be many levels of programmers before an application is developed for the end user to use. That is, one programmer, develop some classes and compile them into libraries. Another programmer writes some classes using the libraries. Another set of programmers uses that libraries to write other classes for others to use or develop programs. The process may go on. So, the term ‘user’ used here may not always refer to the end user. It may refer to the programmer who writes programs using the libraries that others build. (Libraries are resources that we can use in our programs without coding it from scratch. For example, say you want to find the square root of a number. You don’t have to code the function from scratch. You need to simply include that library in your project and then call the function.)

Object orientation is highly analogous to real-world objects. Some real-world examples of the Object Oriented Programming paradigm are:

  1.  Consider a ball. There’s a mold from which the ball is made.  The mold is called the class and the thing that is made with the mold is called the object. We can make as many balls as we like using the mold.  The ball has certain properties, like color, texture, size etc. It has some behaviors like bouncing when dropped, making a particular sound when we knock on it. Those are all behaviors of the ball. The mold allows some properties to be changed for the individual balls. Some properties are fixed and can’t be changed. It’s the mold that should decide it.
  2. Consider a bucket. The mold for making the bucket can be considered as a class and buckets as objects.
  3. Mobile phones. In this case, there’s not a single mold like in previous examples, but there is a complex manufacturing process. The manufacturing process altogether can be considered as a class and the mobile phones as objects.

Object oriented programming is just like this. The properties and methods are bound together in an object.

Now the four pillars of object orientation are

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

1. Abstraction

The complex implementation processes are abstracted away from the user. The users need to know what happens with the code we write. They need not know how it is done. Take the example of the mobile phone. When we play a song, we only need to know what happens when the music file is played. We need not know the complex processes happening inside the phone when it is played. Imagine that we write a class for representing polygons. There are two functions to calculate the area and perimeter of the figure. The user that uses our class needs to know that when the function area is called, the area is calculated. He need not worry about how that is implemented in the class.

2. Encapsulation

It’s very simple to understand. The data and function to operate on that data are encapsulated into a single entity called object. Take the example of the ball. The properties of the ball and the functions of the ball are contained in the ball object itself. However many objects you create, the data and functions are bound to that object itself.

3. Inheritance

Inheritance can be perceived by the literary meaning of the word. It is inheriting something from someone. In our example of mobile phones, the factory at present produces mobile phones with a particular set of features. Suppose we want to add a certain feature to the next model phones and we don’t want to modify the original factory. We create the exact replica of the factory that makes the phones and adds the provisions for the new feature to it. Now, the new factory has all the facilities of the old factory in it, and there are some new features also. The old factory is unchanged. That means we can create old model phones from the old factory, and phones with added features from the new factory. The new phones still have all the features of old phones plus some new features.

4. Polymorphism

Polymorphism is an advanced OOP concept. But it can be simply visualized. As a practical example, the ‘+’ operator in Java, is the addition operator. It is used to add two numbers on the left and right of the operator and return the result. It is also the string concatenation (joining) operator, which can be used to join the two strings on the left and right sides of the operator. Thus the operator is the same, but it performs differently according to the inputs. When the inputs are numbers it works as the arithmetic addition operator and when the inputs are strings, it works as a string concatenation operator. This is an example of polymorphism. There are two types of polymorphism in Object Oriented programming languages. Compile time and run-time polymorphisms. We’ll be able to clearly distinguish between the two when we learn those techniques.

 

Thank you for reading. What do you think about this article? Did I miss any important points? Add your valuable feedback below.

Hi, I’m Shiju P John. A Computer Science Engineering student. I run this site as my hobby and passion.

Leave a Reply