The implementation of object-oriented programming makes complex programs easier to understand which allows reusability of code and reduces the amount of maintenance required. All these become possible because of certain features provided by object-oriented programming.
The features of object-oriented programming are as follows:
1. Object
An object is a collection of a number of entities. Objects take up space in the memory. Objects are instances of classes.
When a program is executed, the objects interact by sending messages to one another. Each object contains data and code to manipulate the data. Objects can interact without having known details of each other’s data or code.
2. Class
Class is a collection of objects of similar type. Objects are variables of the type class. Once a class has been defined, we can create any number of objects belonging to that class.
In other words, class is the blueprint or a set of instructions to build a specific type of object.
3. Encapsulation
Encapsulation literally means to enclose in or as if in a capsule.
Encapsulation is defined as the process of enclosing data and methods within a logical package, called class. It is the mechanism that binds together data and related code in an object to make each object independent of other objects.
Encapsulation leads to the important concept of information hiding, which helps to keep the data belonging to an object safe from outside misuse.
4. Information Hiding
In the context of object-oriented programming, information hiding is a technique of limiting access to variables, methods, and classes in a program. It allows developers to prevent access to non-essential details of an application. The users are allowed to access only the data/methods required by them. This helps the programmer hide the implementation details of an object from a user of the object. Therefore, anyone who wants to use the object for implementing a particular functionality only needs to know how to invoke that functionality, without worrying about how to implement that functionality.
Let us consider an example. While watching television, you do not need to know how the remote control sends the signals to the television for changing channels. You only need to know how to control the television by using a remote. In other words, the internal working of the remote control has been encapsulated. Therefore, encapsulation helps implement information hiding or data hiding because it involves hiding many of the details of an object from the user.
Information hiding is implemented in programs with the help of access specifiers. To know more about the access specifiers, click here.
5. Inheritance
One of the most powerful characteristics of the object-oriented programming approach is code reuse. Inheritance is a process of deriving new classes from existing classes. The existing class is called the base class or super-class and the inherited class is called the derived class or subclass. The derived classes contain all the attributes and behaviors of the existing classes, plus their own attributes and behaviors.
The parent-child relationship between classes can be represented in a hierarchical view, often called a class tree view that starts with the super-class, while derived classes become more specialized further down the tree. Inheritance avoids redundancy in code and enables easy maintenance. Any change made to the base class automatically changes the behavior of its sub-classes. In this case, the sub-classes automatically inherit the new behavior.
Types of Inheritance:
- Single Inheritance
- Multiple Inheritance
- Multi-level Inheritance
To know more about the inheritance, click here.
6. Abstraction
To understand the concept of abstraction in OOP, programmers often ignore all but the relevant data about an object in order to reduce complexity and increase the efficiency of software applications. This process of presenting only the relevant data in a program is called abstraction.
a. Defining Abstraction:
Abstraction is the process of reducing the information content of a concept or phenomenon in order to retain information that is relevant to a particular purpose. This helps to simplify things by isolating only what is relevant for a particular purpose.
In OOP, abstraction forms the basis of building classes. To build classes, a set of similar objects are observed and their common attributes are identified. Next, the attributes of concern to the system in question are included in the class and the irrelevant attributes are excluded.
Before designing a class, you should consider different layers of abstraction. Only the essential details should be defined at the abstract layer, which can form the super-class. The sub-classes of the super-class can be further specialized by adding more details.
b. Benefits of Abstraction:
Abstraction has various advantages, some of which are:
- It reduces complexity.
- It helps in retaining the same interface for implementing different behaviors.
Abstraction deals with providing only the essential details to the users. So, it helps to reduce the complexity of software systems, where a user is required to know only the functionality provided by an abstraction without being concerned about the complexity of its implementation.
Through abstraction, you can implement multiple classes that can have the same set of methods and attributes, but the internal working details of the classes can be different.
7. Polymorphism
In object-oriented methodology, it is possible for an entity to have more than one form. It is depending upon the context in which it is used. This property of OOP, which enables one entity to be used differently for different types of actions, is called polymorphism.
Techniques to implement Polymorphism:
Some techniques to implement polymorphism in an object-oriented program are:
a. Method overloading
Method overloading (also known as function overloading) allows you to assign the same name to two methods that perform similar functions, but take a different number or types of parameters.
b. Operator overloading
In addition to methods, operators can also be overloaded. Operators can be overloaded in order to perform special functions with respect to a class. With the help of operator overloading, standard operations, such as +,-, and *, can be applied to the objects of the class.
c. Method overriding
Object-oriented programming enables a child class. So it provides a specific implementation of a method that is already defined in its parent classes. This is known as method overriding. To override a method present in the parent class, the child class method should have the same name, same parameters, and same return type as the method in the parent class.
When a method is overridden, the version of the method that will be executed depends on the object that is used to call it. If an object of the parent class is used to call the method, then the version in the parent class will be executed and if an object of the sub-class is used to call the method, then the version in the child class will be executed.
Types of Polymorphism:
Polymorphism is divided into two types as follows:
a. Static Polymorphism
In static polymorphism, the response to a function invocation is decided at compile time.
b. Dynamic Polymorphism
In dynamic polymorphism, the response to a function invocation is decided at run time.
Comments: