A Object Oriented Intro
Every Software Engineer is familiar with programming. Already you know, programming is just automating a system. Obviously, by automation we mean instead of carrying out tasks of a system manually, we write certain series of instructions in a sequential manner, which are functionally and logically related. All these instructions work independently but in perfect synchronization and harmony to produce the desired output.
If we believe in the above definition of a program, then we can conclude that in programming, the main focus is on the system to be automated. What we aim at, while writing a program, is to determine the tasks carried out in the system, and in the course, what are the inputs it require, and after processing, the outputs it produces.
Let’s understand this with a real life process. All of you surely enjoy a steaming cup of tea after a tough and grueling day. So, let’s automate the process of tea making. While automating this process, the tasks that has to be carried out are as follows:
1. Taking some water in a container.
2. Putting it on the oven.
3. Lighting the oven and letting the water boil.
4. Once the water has boiled, adding tea leaves to it and waiting for some time.
5. When the liquor is ready, putting sugar and milk as required. The tea is ready.
You can find that for automating the process (of tea making), we concentrated on the process and divided it into certain subtasks. While carrying out the tasks we identify the ingredients (data) and the type of processing (function) required on them.
Now say, one fine evening you aspire for a cup of hot and creamy coffee. Now you have to automate the process of coffee making. But alas! You have to redo it from scratch, even after a tiring day. This is because the process of coffee making is different from that of tea making. The tasks involved in this case are as follows:
1. Putting some milk in a container.
2. Adding sugar to it.
3. Boiling it on the oven.
4. Adding coffee powder to it.
Vao It’s ready.
As you can see that the tasks are different as compared to that of tea making, accordingly some ingredients and processing done on them are also different. Therefore, there is no evasion but to start from scratch, to automate this process.
This is what, is being done in procedural programming. While automating a process in procedural programming, the full concentration is on the process and the associated data required. Whenever the process changes, every thing has to be redone, even if they resemble in some way or the other.
The major features and disadvantages of the procedural programming are written in this article.
Advantages and limitations of Procedural Programming
Procedural approach:
- The problem is viewed as a sequence of actions, such as reading, calculating, printing. Programming language is used to implement those actions.
- A large program written using procedural language was broken down into smaller and manageable sections. Each of these sections is called procedures.
- The procedures worked upon the data present in the programs.
- More stress was given on the functions, rather than the data governed by those functions.
- In multi function programs data were made global so that they could be accessed by many functions.
Certain drawbacks of the procedural language:
- As the program became lengthy, the decision structures became more complex.
- Large changes in code had to be made to bring in a minor change in the program.
- Most of the time programs in procedural languages could not be reused.
- Since data were made global, this led to confusion and accidental data corruption as it was difficult to identify what data was used by which function.
- Programs written in procedural languages were difficult to design because the data used in the program treated separately from the functions that operated on the data.
As you can understand from the above discussion that procedural programming has certain major limitation, which hampers the manageability and reusability of code, therefore new technique of programming evolved, known as Object Oriented.
Programming (OOP)
Object oriented programming is an approach that attempts to eliminate the disadvantages of the conventional programming approach.
Object Oriented Programming is a simulation of real-life processes. In real life, a process is carried on with several self contained and self-sustained objects interacting with each other via their behaviors , which are the integral part of the object. These objects also have certain properties by virtue of which we can easily identify them uniquely.
If we continue with our previous example, of tea making, in Object Oriented approach we will not concentrate on how the process is carried on. Instead we will try to locate the different objects that participate in the process. We will create those individual objects along with their properties and behaviors. Then those Objects will be brought together. Consequently with the exhibition of their related behavior they will interact with each other.
In case of the tea-making process, we will create the objects like water, sugar, milk, tealeaves, oven etc.
The advantage that we will gain from this approach is that while automating the process of coffee making we don’t have to start from scratch once again. We can reuse certain objects like milk, sugar, and oven, which had been already created. Thus it offers us the extreme reusability of code.
The concept of object oriented programming (OOP) was first implemented in a language called Simula. Some other examples of object oriented languages are:
1. Smalltalk.
2. C++.
3. Objective C.
4. NeXTstep.
5. Java
6. VC++
7. C#
And most of the recent and up coming languages are likely to support OOPS.
Features of Object Oriented language:
Languages supporting this concept were designed to develop programs that would combine data and the functions (that operated on the data) into a single entity.
These languages could imitate real-life entities more closely.
Something About Class
The primary idea of the object-oriented approach, in programming was to keep data and functions together.
It is very similar to real life processes. Different objects interact with each other via their methods with the virtue of its properties and give birth to a process. For example, if we consider the process of tea making then different objects like oven, water, sugar, milk etc. interacts with each other and thereby spawns the process of tea making.
Thus, in object oriented programming our main focus will be in creating an object. We can define an object as anything which can be identified easily, with the virtue of properties it posses and it exhibits certain functionality.
In any real life object the properties and functionality (methods) are bundled into a single whole. For example, consider a Ball Pen. A ball pen has certain properties like color, length, diameter etc. On the other hand it also contains certain functionality like writing. All the properties and methods are grouped into the object ball pen itself.
In Object oriented programming the main focus is not on the process, which has to be automated, instead, concentration is on the objects that constitutes the process. For object oriented programming we have to create objects.
In a particular process, many similar types of object can participate. In that case creating several objects individually is a painful stake. Therefore, instead of creating objects individually we create a blueprint of the object. From that blueprint we can create as many objects as we require.
We can consider the example of the cake class. In OOP, the ingredients (data) used to bake the cake and the process (functions) of baking are kept in a single entity. Thus a class can be called a blue print or a template that contains both data and functions that work on the data.
We can take the example of a car manufacturing process. Once a template defining the manufacturing process is made any number of cars can be manufactured by following the blueprint.
A class can be defined in the following way:
Here in the car class bodycolor, number_plate, no_of_seats and engine are the attributes of the class. These attributes or properties can determine the body color of the car or how many seats are there in the car etc.
Now a car can move and can stop. These two operations are done with the help of the property (engine), of the car class. But these two operations can be performed by using some methods only. These two methods are function move and function stop in the class car, which is defined earlier.
Objects
Once a template or a blueprint for manufacturing cars is defined any number of cars can be created following the template. So all the cars that are manufactured are instances of the template or the class that defines the manufacturing process.
These manufactured cars are called objects of the car class.
- An object can be defined as an instance of a class.
- Once a class is defined any number of objets can be created from it.
Let’s take the example of the car class. Objects of the maruti_car class can be defined in the following way:
MARUTI_CAR C1, C2, C3
Therefore, C1, C2, C3 are all objects of the maruti_car class and are independent of each other. The objects will have its own copy of the information that is defined in the class.
We can take the instance C1 and assign values to the data members present inside the instance C1.
C1 -> bodycolor = blue;C1-> number_plate = ABC789556; C1-> no_of_seats = 4;
C1-> engine = disel;
The above statements use the arrow symbol with the name of the object to work with the data.
Characteristics of Objects
Classes are declared to combine together data and functions that work on the data. The data present inside in an object are called the attributes or the characteristics of the object. The functions present in the object are called member functions or methods of the object. The methods determine how the object is going to behave when information is supplied to it. In an object, class attributes are initialized with specific data value.
The main features or characteristics of a class are:
Encapsulation
- It is the mechanism, of binding the member functions and data present in the object together so that it is safe from external misuse. Encapsulation implements two things – data hiding and data abstraction.
- In Object Oriented Programming, the user can classify data as private or public when defining a class. So when objects are declared of that particular class, the data within the object which are private are not accessible to any other class, objects or methods outside the class. The public data within the object is otherwise accessible to outside the class. Thus the object is created in such a way that the public data of the object is used to provide an interface to the outside world to work with the private data. This separating of data as private and public to protect information from accidental corruption is called data hiding.
- The process of data hiding implements another important feature called data abstraction. In OOP, importance is given as to how an object behaves when some information is provided to it. The user is only concerned with the output and not how the output is generated.
- Let us consider the example of riding in a hired cab. The passenger passes the message to the driver about his destination. The passenger is not concerned about the methods the driver uses to take the passenger to the destination. Thus the passenger is abstracted from the mechanism of driving the cab to reach his destination.
Characteristics of Objects
Inheritance
It is the process in which one object can acquire the features of another object. Let’s take the example of defining a cake class. The class can be defined as follows
The above class contains the description of baking an ordinary cake. If the user wants to bake a chocolate cake his task becomes much easier. He can incorporate all the features of the ordinary cake and include the extra information, i.e., the chocolate.
This can be explained by the following figure.
Polymorphism
It is the process where the same object behaves in different ways in different situations.
For example a person named Ram is an instance of the class human beings. He interacts with different people at different times. He behaves professionally in his office and personally in his home. So the same person behaves differently under different situations.
Advantages Of Object Orientation
The main advantage of Object Oriented programming (OOP) language is that it simplifies the development of complex systems.
The advantages of object oriented programming are:
Maintainability
It may so happen that a team of software developers was engaged to develop software. But the people responsible for developing the software are no longer available to maintain the software, which was developed previously. Programs written using OOP techniques are easily understood and hence can be maintained without much difficulty.
Reusability
Let us consider the example of baking an ordinary cake. The process involved in baking an ordinary cake cannot be much different from the process of baking a chocolate cake. Thus a person trying to bake a chocolate cake can reuse most of the process that is required to bake an ordinary cake. As programs written using OOP are more close to real world entities, the concept of reusability can be applied here. Once a standard program is written, it can be reused easily by incorporating minimal changes and more functionality can be added. The same code can be utilized for developing a different project altogether.
Thus reusability implements extensibility of code.
Apart from the above-mentioned primary advantages of OOP’s there are some other derived advantages also.
Extensibility:
In the object oriented approach we can easily extend new characteristics and behavior to an existing object. So the unnecessary duplication of the code can be avoided.
Decomposability:
In this approach, we can split a large program into small modules. Then separate programmers can develop every small part. In this way we can develop and manage a large project easily and on time by distributing the total task.
Understandability:
OOP approach is very much similar to the real life.