Object Oriented Programming (OOPs)

tutorial and example
tutorial and example

Since the dawn of earth we have kept on evolving. The need for evaluation comes with the aim of improving the existing systems when something inappropriate is found for the environment. Same is true in the case of programming languages. We need development in programming languages as well. Before discussing what led to the emergence of OOP, let us take a brief look at what led to the need for such a system.

Starting with the development of first programming language in 19th century we kept on evolving with the needs and drawbacks of the preceding languages.

Then, there emerged a need for a high level language which is structured, efficient and could possibly replace the assembly code. This led to the development of C language which was a Procedure Oriented Programming Language (POP).

Procedure Oriented Programming Language (POP)

In POP, the instructions are written under the main function and each line specifies the instruction to be performed by the computer. When code size increases, the program is divided into several user defined functions. In POP, the data is given the second priority. The codes in POP were written for a specific purpose and could not be used elsewhere.

C language shook the computer world; it appeared as if a perfect language has been found which is well equipped and sufficient for the development of system code.

With every perfect thing there come some disadvantages, same was true for C.  Codes in small and medium size were easily handled, but the long sized codes became difficult to manage and manipulate.

Disadvantage of Procedure Oriented Programming Language (POP)

There were following disadvantages which were faced with this approach of programming:

  • Every function could access the global variables; hence necessary changes were required to be made in function definition so that manipulation of data values can be done only when required.
  • This approach of programming was not suitable for real life problems.
  • Adding new features to existing code was a daunting task.

In order to track the above mentioned issues and overcome from them there was a need to develop a system of programming in which:

    • The emphasis is mainly on data.
    • Once written could be reused for other purposes.
    • New methods and variables could be added to the existing code whenever required without the need to write the whole code.

And hence the object oriented programming system was developed.

Object-oriented Programming

Object Oriented Programming is an approach in which the programs are standardized. A special memory area is created for the methods and data. This approach of programming revolves around ‘objects’, which are nothing but real world entities. This methodology of programming helps to organize complex programs using concepts of inheritance, polymorphism, and data abstraction. In simple terms, the approach in OOP is to firstly create a class that has a large number of methods and variables and then using the class the state of objects are determined.  Hence class is nothing but a blueprint for determining the state of objects and objects are the real world entities.

Let us take a look at the concepts of OOP

1. Class:  Class is a blueprint to give shape to the real world entities called the objects. A class contains a number of variables and methods. When the objects call these method and variables there state, existence and structure are defined. Class doesn’t have any physical existence and they do not occupy any space on their own.

E.g. Fruit, Building, Shape, Vehicle, etc.

2. Objects: They are the soul component of the OOP. They are real world entities with a state and a behavior. Any program is analyzed with the help of objects and their behavior. Objects are associated with classes and they obtain their structure from the class. There can be more than one objects associated with the same class. Each of these objects can communicate with each other. Objects occupy their space in memory.

E.g. mango, banana, apple, car, bus, person, etc.

3. Inheritance: A very important concept of OOP is inheritance. It is property by which a class can succeed the other class. It supports hierarchical programming. We have a class which has some methods and variables defined, if there is a need to make a class which has all the properties of the previous class with some additional features of its own then it can be achieved by inheritance. The class which is previously defined is the parent class or the super class and the class which inherits the features of the previously defined class is called the child class. For e.g.

In the figure shown above, all the features of polygon are present in quadrilateral and hexagon. Quadrilateral later extends to rectangle and kite.

4. Polymorphism: Poly means ‘many’ and ‘morph’ means form. So polymorphism is the ability to take many forms. For example remember the childhood days when while watching ghost programs the ghost was able to take the shape of any human being as well as animal or any other possible object as needed by him. This is what polymorphism means. An entity can take several different forms as and when required.

There are two types of polymorphism:

    • Compile time polymorphism
    • Run time polymorphism

5. Encapsulation: Encapsulation is the act of wrapping up or binding of the data and code together into a single entity. It is very important as it helps to achieve data hiding and data security in our code. It can be achieved by many ways one of them is to use the various access specifiers provided. Other way is to use nesting of class, the class which contains the private code is covered by another class which can only access the private class.

6. Data Abstraction: It is an act of showing only the necessary information, hiding the background details. We use abstract class and interfaces in java to achieve data abstraction. It is also an important concept as the users only get to see the relevant information not the internal functioning.

E.g. when we only mail account, internal processing is not shown.

Benefits of Object-oriented Programming

  1. Code once written can be reused further.
  2. With the help of inheritance whenever there is a need for a class extension, it can be achieved.
  3. Since the code is divided into methods and variables, adding features to the existing code is easy.
  4. Error detection and correction is easy.
  5. There can be various instances of the same code at the same time.
  6. Up gradation is easier.
  7. Code which needs to be kept secure from the outside world can be significantly achieved.
  8. Code redundancy does not occur.
  9. Software complexity can be easily managed.
  10. Work partitioning is easy in this approach of programming.