Monday 29 August 2011

Object Oriented Programming (OOPS)

1. Classes
One of the major problems in the earlier approach was also the data and the
functions working upon the data being separate. This leads to the necessity of
checking the type of data before operating upon the data. The first rule of the
object-oriented paradigm says that if the data and the functions acting upon the
data can go together let them be together. We can define this unit, which
contains the data and its functions together as a class. A class can also be
defined as a programmatic representation of an entity and the behavior of that
entity can be represented by the functions in the class. In our earlier case study
the employee, can be represented as a class. A class will contain its data into
various variables, which would be termed as data members and the behavior of
the class, which will be encapsulated, as functions will be termed as member
functions.


2. Encapsulation
Many a times when we use certain tools, we hardly pay attention to the details
about the functionality of the tool. We hardly pay attention to the various other
units, which make up the tool. This behavior to ignore unwanted details of an
entity is termed as abstraction.
Now if the details are unwanted why show them to the user? Therefore, the
creator might attempt to hide these unwanted details. This behavior is termed as
encapsulation. So we can say that encapsulation is an implementation of
abstraction. Encapsulation directly leads to two main advantages:

  1. Data Hiding: -- The user of the class does not come to know about the internals
    of the class. Hence, the user never comes to know about the exact data
    members in the class. The user interacts with the data members only through the
    various member functions provided by the class.

  2. Data Security: - Since the data, members are not directly available to the user
    directly but are available only through the member functions a validity check can
    always be imposed to ensure that only valid data is been inserted into the class.
    So a Date class, which contains individual data members for storing date, month
    and year, will have it ensured the month is never 13 or the date is never
    exceeding 31.
3.  Inheritance
 What is common between a father and a son? At least one thing would be
common – their assets!
In real life, inheritance allows us to reuse things that belong to a particular entity.
Also, in object oriented world a class can inherit the properties and functionalities
defined in some another class so that they can be reused. Then we have to be a
bit careful in designing these classes because reusability cannot be done unless
the classes are of the same type. So the class which would be reusing the
functionalities of the other class in object oriented terms we would say that the
class is “deriving” from the former class and is termed as the derived class. The
class that is being “derived from” is termed as the base class. Inheritance
directly results in the following benefits: --

  1. Reusability: -- Inheritance results in functionalities defined in one class beingreused in the derived classes. So the efforts of rewriting the same functionalityfor every derived class is being saved. This definitely saves a lot of developmenttime.
4. Enhancement and Specification: -- Due to the characteristic of inheritance, we
can club the common functionalities in the base class and have the specific
functionalities in the derived class. This feature can be used to have a
functionality defined in the base class to be further modified for betterment or
specification by the derived class. This mechanism of redefining the functionality
of the base class in the derived class is termed as “overriding”

Avoiding type inspection:-- In the case study that we discussed to understand
procedural approach we had a strict type inspection routine at the library end
wherein in every function in the library we had to check for the type of the
employee for whom the work has to be done. With inheritance, we would have a
common base class called as “Employee” which would have all the common
functionalities defined where as the specific routines do be done for various types
of employees would go in the respective derived classes. So there would be
class defined for every type of employee and the class would all the
specifications for that type of employee and would be derived from the base
class Employee to inherit the common functionalities. Therefore, in the functions
now we wont have to check for the type of employee every time because every
employee type has its own specific routines defined within it.
 
5. Polymorphism
The word “polymorphism” means “different forms”. Applied in object-oriented
paradigm it means the ability of an entity to exhibit different forms at runtime.
However, why would such a kind of feature be required? One major reason to
have this is to eliminate the type inspection. As we can see in the earlier case
study that we discussed there would also be a type inspection checking at the
client application level where in the employee entities would be used. So just as
in every functionality, we had checked for the type of employee we will also have
to check in the main function about the type of employee we are handling. With
polymorphism, we can have this level of type inspection also being eradicated
totally.

No comments:

Post a Comment