Benefits of Object Oriented Programming (OOPS)
three major features of object-oriented programming are:
is a good practice to provide a coupling between the data and the methods that
operate on the data (method is another name for a function or a procedure, also
called an operation). Such data should be hidden from the outside world; this
means it should be inaccessible to code outside the current context (to be more
precise, the current object). This process of information hiding and combining
data and methods in a single logical unit is called encapsulation. We
say that the data and the methods that operate on this data are encapsulated
into a single unit called a class.
class consists of data (more precisely called attributes) and methods. These
are called the members of the class. The attributes of the class should be
considered “private” to an instance of a class; only the class methods
would have access to these attributes. When we define a class, we can set the
visibility access to these attributes. They may be made visible to the code
outside the current class definition. We encapsulate the data and the related
methods in a logical unit called a class.
already know that a class contains attributes and the methods that operate on
them. A class acts like a template. It is the basis on which different objects
are created. Each object possesses data unique to it; however, all the objects
of the same class type possess the same characteristics. For example, when we
create an Employee class, each Employee object will contain the same data
attributes, such as ID, name, base salary, 401(k) plan, leave travel allowance,
and so on. The values assigned to these attributes will vary from employee to employee.
Each Employee object exhibits the same functionality that is defined by the
methods of the Employee class. At some later time, we may want to represent a
manager in our software.
this, we would create a Manager class that inherits the characteristics of the
Employee class; after all, a manager is also an employee.
nature, we observe that children inherit some traits from their parents. We
find various families of classes in nature, such as birds, animals, mammals,
and so on. Each family consists of several objects. All objects in a given
family share common characteristics (or in the context of object-oriented
programming, a common functionality). The children in the family inherit these characteristics
from their parents. A child may also exhibit characteristics (functionality) in
addition to what it has inherited from its parents.
software engineering, when we develop software, we search for the presence of a
family of classes similar to what we observe in nature. For example, to represent
different types of cars in a software application, we may design a parent class
called Vehicle. The class Vehicle defines functionality that is common to all
automobiles. We can further define classes based on this parent class
(Vehicle), such as Car, Truck, and so on. Each such class adds some functionality
to the functionality inherited from the Vehicle class. The added functionality
is unique to the defining class. This means that a Car and a Truck will add
some functionality to Vehicle that is different from the functionality of the
other classes in the same family. For example, a Car may describe the passenger
capacity as its characteristic, whereas a Truck may define the maximum load
capacity as one of its added characteristics.
Car may be further classified as a sports car, a passenger car, a sports
utility vehicle (SUV), and so on. We can define classes for these
classifications. An SUV class will inherit from the Car class, which in turn
inherits from the Vehicle class. Thus, SUV exhibits the functionality of not only
the Car class but also the Vehicle class. The concept of inheritance helps preserve our
investment in existing code by allowing us to extend the functionality of
our classification of automobiles, we have different classes in our class
hierarchy for the Car object. Each such Car object exhibits several common
functionalities. For example, a “drive” method could be defined in each class
that is applicable to all the Car objects. Because this is a common
functionality, we should define this in our parent class. The child class will
also define a “drive” method and may modify the inherited “drive” method. Both
methods may use the same name (that is, drive). Driving a truck is different
from driving a sports car or an SUV. However, we may say, “we drive the
vehicle” with regard to both the vehicles. Thus, the name of the functionality
remains the same, even though the implementation varies across objects.
feature is called polymorphism in object-oriented terminology. Polymorphism
originates from the Greek word polymorph, meaning having different faces to the
that we have examined the three main features of object-oriented programming,
we can see how OOP helps us in:
programs where the code, once developed, is easily reusable.
Programs can be easily
extended with little effort, allowing us to reuse the existing code (which has
been previously tested)
And thereby reducing the
maintenance cost for the software.