Features of Object Oriented Programming
Objects are the basic building blocks of Object Oriented Programming System. The real world objects have tow characteristics: State and behaviour. For example, human beings have state, such as name, color, age and height and behaviour such as walking and dancing. All the Objects in OOPS are based on real world having a specific state and behaviour.
Object Oriented Programming System consists of the following features:
Let’s explain each of them in details.
Grady Booch, a famous author who has written books on OOPS defines Encapsulation as the process of hiding all of the details of an object that do not contribute to its essential characteristics.
Encapsulation implies that the non essential details of an object are hidden from the user and an access is provided to its essential details. Therefore Encapsulation is also called information hiding. For example, computer games also use the feature of encapsulation. The player of the game only needs to know how to play the game, however the complex working of how the game works is hidden from the user.
Another example is that of a vacuum cleaner. When a vacuum cleaner is plugged to electricity, the cleaner starts functioning. The user need not know the working principle of a vacuum cleaner to convert electricity into suction power. The switch of the vacuum cleaner encapsulates the complex process of conversion of electricity into suction power. The complexity of an object is hidden as a result of encapsulation. In object oriented methodology, need for encapsulation arises because the emphasis is on designing classes in such a manner that the classes share data and methods among themselves. Encapsulation is the feature that provides security to the data and the methods of a class.
Grady Booch defined the Abstraction feature as:
An Abstraction denotes the essential characteristics of an object that distinguishes it from all other kinds of object and thus provides crisply defined conceptual boundaries, relative to the perspective of the viewer.
Abstraction refers to the attributes of an object that clearly demarcates it from other objects. Abstraction enables you to provide a restricted access to data. Abstraction also means ignoring the non-essential details of an object and concentrating on its essential details. An example could be a typical ATM machine, when a user visits an ATM machine; the user is not required to know the entire process of the transaction and how the data is stored. He only needs to know how to key in the required information’s to initiate the transaction. However, the program provides a restricted access to the user’s account. Similarly, when you want to send an e-mail message, you only need to know the process of writing and sending the massage to the receiver. However, it is not necessary for you to know the entire process of sending the e-mail messages across the network.
To implement abstraction, you also use the encapsulation feature. Encapsulation hides the irrelevant details of an Object and abstraction makes only the relevant details of the object visible. For example, the operation of a washing machine is hidden or encapsulated from the user. The details, like switching on and off the washing machine are the only details relevant for the user to know. This is implemented by abstraction while rest of the working of the machine is encapsulated from the user.
Polymorphism is derived from two Latin words, Poly which means many and morph which means forms. Anything that exists in more than one form is known as a polymorph. An example to explain this could be how a single person assumes different roles in a family. One person can be a father to his children, child to his parents and husband to his wife. Although the person is same but he behaves differently with his family members as per relation with the member.
In OOP, polymorphism enables an entity to have more than one form depending upon the context in which it is used. Polymorphism enables one entity to be used as a general category for different types of action.
In object oriented programming, inheritance enables you to extend the functionality of an existing class. You create a class that inherits the attributes and behaviour of another class. In addition to the attributes inherited from other classes, the new class can also consist of its own new attributes and behaviours that are specific to the class. Inheritance enables you to add new features and functionality to an existing without modifying the existing class, inheritance also enables you to share data and methods among multiple classes
Superclass and Subclass
In inheritance, a superclass or parent class is the one from which another class inherits attribute and behaviours from. A subclass or child class is the class that inherits attributes and behaviours from the Superclass.
The following are some characteristics of a Subclass.
- A subclass is a specialized form of a superclass
- A subclass can inherit the properties and methods of a superclass.
- A subclass may have additional features in addition to the once inherited from its superclass.
We have two types of inheritance namely the Single and Multiple inheritances.
In single inheritance, a subclass inherits attributes and method from a single superclass while in multiple inheritance; a subclass inherits attributes and methods from more than one superclass. A typical example of multiple inheritances can be the parent-child relationship; a child inherits characteristics of mother and father.