![]() |
|
OBJECT ORIENTED PROGRAMMING (OOP) – INTRODUCTION
What is the meaning of term ‘Object Oriented’ and what is the need for it? Well…! To understand this first tell me what do you do when the battery (cell) of your torch (or any other device) runs out? Probably, you simply replace the battery with a new one. Is it? Now, what do you think, you know everything about the battery and the torch. I mean their architecture, functionality, etc. If, by any chance, you know also then does everybody who replaces the battery know about the details of the battery and the device? Obviously not. Now think about other things that we come across in our daily lives like Cars, Computers, Telephones or anything. Whenever something breaks down it is probably repaired by the people who themselves do not know much about the internal engineering of the object. For example, a car and a car mechanic; torch and you, etc. What we have noticed in above paragraphs is that many things in life are easy to use and maintain because to use (or repair) them we do not need to know about their internal details. I mean we don’t need to understand the engineering of car to drive it… What we achieve by this phenomenon is revolutionary. Do you agree? Now what is this phenomenon basically? See, if we think for while, we’ll come to know about two activities done by this phenomenon:
- Hiding the complexity of a system
For example, in a torch an interface is provided for putting the battery and same is the case with battery which has interface which we can connect to the torch. This way we easily link the two systems. This phenomenon is named ABSTRACTION. Because of this abstraction the life becomes easy or rather possible. If abstraction were not there, we would have to learn the chemistry of bread to eat it. The equation of water to drink… At the root of all, we would have to learn about atoms, electrons, etc. before using anything… Do you think we could survive in such a condition? Because of this Abstraction we are where we are now… And because of this abstraction only, we are able to make progress, grow… I hope you do agree with me? Is it? If not, please discuss… so that we can come to a common conclusion… ... Abstraction and Programming ... The concept of Abstraction holds true for programming also. See, what does Abstraction mean? It means hiding the complexity of a system and providing a simple interface for it. If can be able to achieve Abstraction in programming, we can make the same progress as real life in programming also. This can open a window to the infinite possibilities. We will just make many systems and hide their complexities so that they can easily be used anywhere by any one and this will give the scope of doing virtually anything… The concept of abstraction is the basic idea behind the Object Oriented Programming Paradigm. And because of this reason, this programming model is most powerful and most used model in the world of programming. This makes programming very powerful and yet very easy. And so makes our lives, as a programmer, very easy. Basically, OOP suggests that a system must be made from simpler, independent units which are capable of handling information and functionality related to them independently. Such units are called Objects. The whole OOP revolves around such objects, hence the name ‘Object Oriented’. In OOP model, we create and use ‘Objects’ to make our code much cleaner, efficient, robust, easy to manage and maintain, and debug. Now, I hope, you understand the meaning of OOP and the need for it? ... Classes and Objects ... You are already familiar with the concept of an Object. An object, as we have already seen, is a piece of code which is capable of handling information and functionality related to it independently. We can visualize objects as small components of a bigger system, which can be grouped together to develop the system. For example, Computer is a system and the Hard Disk, RAM, Motherboard are different objects used in a computer system. Often, the objects represent some entity which is able to exist independently. If we want to use some object in our program, we only need to know what does this object do and not how it does it….Abstraction again! To use it, we will have to load the object in memory so that it can use the CPU and other resources or simply run! Now, loading an object into memory is called Instantiation, in other words, whenever we load an object we can say we have created an instance of it. Technically, an object is called an object when it is in an instantiated state or loaded. This is so because then only we can actually use it… But there must be some definition for this object, which describes the functionality of the object. This definition is called the Class. Please note that a class is not an object. It is just a set of instructions telling how an object should work. To understand this let us take an example. All of you must be familiar with the house map. It contains the details of a how to make a house, where to put windows, where to keep the doors, and how many rooms should be there, of what sizes, etc. Is that ‘map’ the house itself? No. A map is just the instruction set and what we create from it is a house, which is a physical/actual thing made up of cement and concrete which can be used to live in. The same relation exists between a class and an object. Map \-----\ House
I hope now you have a clear about the class and the objects. An object is a programming unit and class is definition of the object. If still not clear, please discuss. Now, let’s see some of the terms associated with the Object Oriented Programming Paradigm. ... Encapsulation ... This is nothing but what we have already seen. This concept simply suggests that the code, that is, the data and the functionality must be encapsulated in a ‘black box’. Or simply we can say that the actual code must be hidden from the users of the code. Practically this is what happens when we use Objects. We don’t actually see the code behind an object, we can just use it. This concept is also called ‘Data hiding’. ... Inheritance ... To understand the meaning of Inheritance in programming context, let’s see an example. Let us consider that there is an Animal class. It has all the features of a general animal. When I say general animal, I mean not any particular animal. The class defines 4 legs, 2 eyes, 1 tail, etc. and also the functionality like Sleeping, Running, etc. Now, if I need to get a lion, I don’t need to write the whole class all over again. I can just inherit my lion class from the animal class. When I do this I will only have to write code for the additional features of Lion like Roaring, etc. This facility is available in the Object Oriented Programming Paradigm and it greatly supports code reuse. We can inherit from a Superclass and the class which inherits a superclass is called a Subclass in OOP. ... Polymorphism ... Polymorphism in OOP means ‘same name different functionality’. What this means in action is that we have a same name for different functionalities. For example, we have the same name – ‘Sleep’ which we can use for making each animal sleep, be it tiger or lion or cat or anything. What I mean is that the method for sleeping is different in each of these cases/animals… Cat sleeps differently than a tiger and so on, but we still use ‘Sleep’ to make all animals sleep. This means that we have different functionalities but having the same name that is Sleep. This is Polymorphism (many + forms). There are two terms generally used in this context:
Overloading Whenever we introduce/write a new functionality with the same name as an existing functionality but changing the signature, we are implementing Overloading. In overloading we can differentiate between the two functionalities by their signatures. And the call made to that functionality depends on the context. This way both the functionalities can co-exist. Overriding Whenever we write a new functionality with the name and signature both same, the existing functionality gets overridden by the new functionality and hence can’t be use. When a call to the functionality is made, only the new functionality is called. This is called Overriding. This was just an introduction to the concept of OOP and the various terms used in it. But the real fun is in using these concepts… |