You might have already heard of the term ‘Object Oriented Programming’ if you’re related to technology field in some way. But do you have a clear concept of what is object orientation? This post is for giving you an idea of what is OOP and what it is not.
We will be able to find attempts to describe object orientation by examples of animals or vehicles in books. That approach may not help everyone. Here I’ll try a different approach to make you understand what object oriented programming is.
Imagine we are programming for GUI. That means we will use the graphical elements like buttons, text boxes, checkboxes etc.
This is a button, which the user should be able to click with the mouse.
Now, this button may have certain properties like size, color, shape, text color, text size etc.
It may have certain behaviors that we define in our program. For example, when we click on it it should perform some action. Or when we hover the mouse pointer above it, it should perform an action like changing the color, border etc. to indicate that it is clickable. All these are the behaviors of the button.
In object oriented programming, we bind together the properties and behaviors. In other words, object-oriented programming allows you to bind together properties and behaviors of an object as a single entity. We use classes for this.
Classes are the core element of any object oriented language. Consider a class as a blueprint for an object. In other words, we create objects of the type class. In this example, we create a class for the buttons. It has all the properties and behaviors of the objects (in this case, buttons) created defined in it. Each time we want to create a button in our program, we create an object of that particular type. Thus a button object is created from the blueprint(class) we first define.
In a procedure-oriented language, it’s not possible to do it this way. It may be possible to program for GUI, depending on the language. You may first feel like it’s simple to use a procedure-oriented language. But as your program grows in size this approach will not be efficient. Consider the case of a program in which you use fifty buttons. In the object-oriented environment, you have to define a class and then we have to create fifty objects of that class type. With a procedure-oriented language, this is not possible. We have to write the logic of each of these buttons separately which is a tedious work.
Inheritance is an important feature of object orientation. Do you know what are the practical benefits of inheritance? Let’s take a look. Take our earlier example of buttons. Imagine you have to create a new type of button now. It has all the properties and behaviors of the buttons we used earlier, plus some additional behavior to define what happens when a user double clicks on it.
Here, in this case, we can consider our old class as a parent class and define a new class as a child class of it. The child class will inherit all the properties and behaviors of the parent class automatically and we add a new behavior to it. Now the objects of type child class will have the double-click behavior defined. This is the way inheritance work in object oriented programming languages. It is simple and efficient, and it avoids code redundancy to a great extent.
What do you think of this write up about object oriented programming? Did I miss any relevant points? Add your comments here: