Learn Programming with Fun

Features of Object Oriented Programming

Object-Oriented Programming (OOP) is used to build OOPS. Objects are the basic building block of Object-Oriented Programming System (OOPS). The real world objects have two characteristics: state and behavior. For example, human beings have state, such as name, color, age, and height and behavior such as walking, dancing, and sleeping. All the objects in OOPS are based on real world having a specific state and behavior.

OOPS consists of the following features:

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

Encapsulation

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, when you plug in the cord of a vacuum cleaner and switch it on, the vacuum cleaner starts functioning. An end-user need not know the working principle of a vacuum cleaner to convert electricity into suction power. The switch of th6 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.

Computer games also use the feature of encapsulation. The user only needs to know how to play the game. However, the complex working of game is hidden from the user.

In object-oriented methodology, need of 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.

An Anytime money (ATM) that you use to withdraw money also has an encapsulated function. You insert an ATM card in the machine and provide the pin code. The only access that you have to the ATM is to provide the pin code for your account and not to the process of validating the password. If the pin code is correct, you can make the desired transaction. The backend transaction process is encapsulated. In addition, if you try to access another account, access is denied, as you are not authorized to access any account other than your account.

Abstraction

Abstraction refers to the attributes of an object that clearly demarcates it from other objects. For example, while developing an online shopping site for books, you use objects of various items, such as books, compact disks (CDs), and cassettes. All these objects have a well-defined set of attributes that distinguish these objects from each other. For example, a book has page numbers, type of binding, and subject. However, a CD has type of media, such as audio or visual, duration, and storage capacity of the CD.

The concept of abstraction is implemented in object-oriented programming by creating classes. All the attributes of the objects of the classes are defined in the class. However, you cannot store any data in a class because creating a class does not allocate any memory space to the class. To store data, you need to create objects of the class, which have memory allocated as soon as it is created. Classes form the templates for creating objects.

In addition to the well-defined characteristics of an object, abstraction enables you to provide a restricted access to data. You come across hundreds of advertising messages every day through magazines or newspapers. You do not read, understand, and respond to all of these because these are not of your interest. Instead, you concentrate only on messages that are of your specific interest. For example, if you want to buy a refrigerator, you will concentrate on the advertisements featuring refrigerators. You will not concentrate on the advertisements of other products, such as vacuum cleaner, washing machine, and air conditioner.

In object-oriented programming, abstraction means ignoring the non-essential details of an object and concentrating on its essential details. As discussed earlier, in an ATM, the user is not required to know the entire process of a transaction and how the data is stored. However, the program provides a restricted access to the user’s account.

Similarly, when you want to send e-mail messages, you should know the processes of writing e-mail messages and sending it 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 an 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. Rest of the working of the washing machine is encapsulated from the user.

 

Inheritance

In object-oriented methodology, inheritance enables you to extend the functionality of an existing class. You create a class that inherits the attributes and behavior of another class. In addition, the new class can consist of a few new attributes and behaviors that are specific to the class. In terms of classes and objects, attributes refer to the data and behavior refers to the methods.

For example, you might have the Student class that consists of the basic characteristics of students, such as name, date of birth, and gender. You can extend the functions of the student class to a day scholar, who in addition to characteristics of the Student class contains its own features, such as locker number and residential address.

Inheritance enables you to add new features and functionality to an existing class without modifying the existing class. Inheritance also enables you to share data and methods among multiple classes.

 

Polymorphism

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. You might have noticed that a person assumes different roles in different scenarios. For example, a person assumes various roles when he interacts with the members of his family. He is a father to his children, child to his parents, and husband to his wife. Although, the person is same, he behaves differently with the family members as per his relation with the member.

In object-oriented methodology, polymorphism is the feature that enables you to assign a different meaning or usage to an entity in different contexts. The entity can be a variable, method, or an object. In other words, Polymorphism allows a programmer to make use of an entity in several different forms without affecting the original identity of the entity.

Rclretail

View more posts from this author