The evolution of the object-oriented programming approach is cited with the overcome of several flaws in the existing procedural-oriented programming methodology. The OOp or the nonprocedural programming approach activates data as a critical element in the programming modulation and does not allow it to move freely around the system. It allows the decomposition of the problem into different entities known as objects and builds data and functions around these entities. Procedural programming aims more at procedures whereas object-oriented programming is based on data and depicts problems in relation to classes and objects involved.
Following are the characteristics of Object-Oriented Programming:
- Emphasis is on data rather than functions.
- Programs are divided into sets of related objects.
- Data structures are designed in such a way so as to characterize the objects.
- Functions that operate on the data of an object are tied together with the data structure.
- Data of an object is hidden and cannot be accessed by external functions.
- Objects communicate with each other through functions.
- Employs a bottom-up approach to program design.
The organization of programs using an object-oriented programming approach overcomes all the problems of procedural programming.
Hence object-oriented programming can formally be defined as a method implementation in which programs are organized as a cooperative collection of objects, each of which represents an instance of some class, and whose classes are all members of a hierarchy of classes united via inheritance relationship.
Features of Object-Oriented Programming:
(1) Objects- We understand the world around us through the objects. We see the environment as being composed of various objects which have an identity (data) and behavior (functions). We can classify individual objects according to their common characteristics. For Example- Let us consider automobiles. Although there are individual differences among automobiles such as cars, motorcycles, buses, etc., we consider all of these belong to a general class of ‘automobiles’. The fundamental idea behind object-oriented languages is to combine data and functions (that operate on that data) into a single unit. Such a unit is called an object. An object is not necessarily a physical object such as a car. It can be an identifiable object like a bank account. Its concept depends on how we use our imagination to design a program in terms of objects. Its data are called data members. Its functions are called member functions.
(2) Classes- In object-oriented programming, a class is a set of data and functions- the functions that have to be executed on this data. A programmer specifies a class as per the requirements of the problem. For example- if he is writing a program where it is expected to enter the roll number, name, and marks of a student and then display them, then the data will be roll number, name, and marks whereas there will be two functions one to enter the values for these data and the other that will display the values entered by the user.
(3) Data Hiding and Encapsulation- An object’s functions provide the only way to access its data. If a data item in an object is to be read, the specific member function is called. That member function reads the data item. Data members cannot be accessed directly. Thus the data is hidden.
Data hiding does not mean data security. Computer databases are generally protected from unauthorized users by providing passwords to authorized users. But data hiding is a concept in object-oriented programming design to protect the private data of an object from accidental alteration.
Encapsulation is one of the fundamental concepts of object-oriented programming. It is a way of combining data and functions (that operate on the data) into a single unit.
Once a class is defined, objects can be created for that class. Each object has its own copies of the class data elements. The functions defined for the class can be called by objects of that class. This is called sending a message to the object. Messages can be sent to any object. However, only the object receiving the message acts upon that message. Objects are independent of each other. Therefore, changes to the data items of one object have no effect on the data items of other objects. Similarly, sending a message to one object has no effect on other objects.
(4) Inheritance- Inheritance describes the relationship between two classes. It is the ability to create classes that share the attributes and methods of existing classes, but with more specific features. Inheritance is mainly used for code reusability. It is the process of forming a new class from an existing class or base class. The base class is also known as the parent class or superclass, and the new class that is formed is called the derived class which is also known as the child class or subclass. Inheritance helps in reducing the overall code size of the program, which is an important concept in object-oriented programming. So you are making use of already written class and further extending on that. In general, one line definition you can tell that deriving a new class from an existing class is called inheritance. For Example- Let us think of an organization. It consists of various types of employees: managers, engineers, technicians, and so on. We can take employees as a base class and we can derive a new class engineer. In this case, the employee is the base class and the engineer is the derived class. A system of base and derived classes is known as a class hierarchy.
(5) Polymorphism- One of the important concepts of object-oriented programming is polymorphism. It can be used with a system of base and derived classes. Polymorphism is the ability for member functions in a class hierarchy to perform different operations for the same message depending on the type of the object for which the member function is called and without regard to the class type of the reference to the object. For Example- Let us consider the base class employee. Two classes engineer and technician are derived from employee. Both the derived classes have the same member function net pay to calculate the net pay. While calculating the net pay, income tax has to be deducted in the case of an engineer, and a uniform allowance has to be added in the case of a technician. Thus the calculation of net pay in both cases is not the same. Though the member functions are present in both the classes engineer and technician, the net pay is calculated differently when a message is sent to their objects with a reference to their base class employee.
Our discussion on polymorphism relates to function overloading. There is another type of overloading, known as operator overloading. Both function overloading and operator overloading are also considered to be a kind of polymorphism.
Advantages of Object-Oriented Programming:
Object-Oriented Programming is a new way of looking at problems and their solutions. This approach is very close to the real world and therefore offers many benefits and better quality of software. With less amount of maintenance involved, the advantages of Object-Oriented Programming can be summed up as follows:
- Software Reusability- When we develop software we develop it in terms of objects. Objects are data abstraction. Hence they have some existence outside the program. When we write another program of similar nature, all these objects can be directly used. It means that half the job is done. Our efficiency is thus doubled.
- Code Sharing- If we resort to some standardization, if our friends (or members of a software team) have developed some code, we can directly use it in our program. No tuning or testing is required. In our opinion, this concept and the concept of software reusability are the same except for who wrote that software. Reusability refers to using our own code, while code sharing refers to code written by our friend.
- Rapid Prototyping- Object orientation helps in rapid prototyping. When the basic design is done, elementary objects can be designed with a minimum of methods. Only at a later stage can all the methods be developed.
- Information Hiding- This is the most important advantage of Object-Oriented Programming. It adds a lot of safety to software. Here we are not referring to the lock and key type of safety. We are referring to inadvertent errors. They make it difficult to diagnose bugs. It takes up a lot of project time. Thus, a considerable amount of time is saved because the compiler does not allow us to make many such errors.