Reference Book is : Bala Guruswamy
Introduction
Object Oriented Programming is a style of
programming. Programming evolved from bit/bytes level, assembly code, and
procedural, structured and then to object oriented programming. Each of these
earlier ways of programming had their own drawbacks. Some of which are listed
below.
·
Emphasis is more
on the process, rather than on the data.
·
It is very
difficult to separate the functions because they are tightly interwoven and
interdependent.
·
Modifications of
a function could mean that the entire application had to be recompiled.
·
The task of
coding, debugging and maintenance is very complex.
·
They are not
suitable for very complex applications for they do not model real life situations.
·
Achieving dynamic
behavior is not an easy task.
·
The concept of code
reusability is not present.
·
Development of
software and the process of maintenance are very expensive.
To address these problems, OOP was first
thought of in Simula in 1968 and later in C++, a hybrid language developed by
Bjarne Stroustrup. Now Smalltalk and java are full-fledged OOP languages.
OOP Features
Any OOP language should support the following
features
1.
Object
2.
Class
3.
Abstraction
4.
Encapsulation
5.
Data Hiding
6.
Messages
7.
Polymorphism
8.
Inheritance
9.
Code Reusability
Objects
An object
is nothing but an instance of a class. The fundamental concept in OOP is an
object, which is an entity that has an existence. Example of objects could be
cars, books, files stacks etc. Some of them can be felt and while others
cannot. However we are not interested in all objects, our concern is only of
those that exist in our problem domain. An object has fundamentally the three
characteristics.
An object has
·
A state
·
A behavior
·
An identity
For example: we need some information about
cars. Cars can have the following features.
State : Color, Size, Weight, Engine capacity etc.
Behavior: Start, Stop, Accelerate
etc.
Identity : Registration number, Chassis number etc.
These characteristics define the car, but
these things have different meanings to different people. A mechanic in a
garage will look at it differently as compared to a traffic police or the owner
who drives it. These represent problem domain. Therefore relevant features that
are same of importance depending on whose is using it.
Objects such as a car can be simple or they
can be extremely complex. Object may be real or imaginary.
Examples:
Real object- Fiat, car, java classroom etc.
Imaginary: record,
organization etc.
Complex: databases and
spreadsheets.
Software object – in software terms an object is a bundle of
variables and related methods.
Examples:
Variables – bank account number, bank account
holder’s name
Methods: deposit, withdraw, etc.
Methods
A software object is nothing but a collection
of variables and related methods.
Class
A class is a blue print that defines the variables
and methods common to all objects of a certain kind. For example, let us
consider different kinds of cars such as Fiat, Maruti etc. All of them have a
general behavior and state. Fiat car is a particular case of the general category
of the cars. Here ‘cars’ is a class. Similarly for the class bank accounts we
can have savings bank account, current account etc.
Therefore a class only forms a blue print or a
prototype based on which objects can be derived. An object is a particular
instance of a class. For instance a fiat car bearing registration number AP
09D 3233 is a particular case of the class cars. It has a unique identity. A
car with a different number is a different of the object of the class cars.
In the above diagram:
Variables:
Accelerator, Brakes, Gear
These variables define the state of the objects of
the class car.
Methods:
Accelerate, Apply_Brake, Change_Gear
These methods define the behavior for the objects of
the class car.
Characteristics of instance variables and methods
1.
An object is an
instance of a class.
2.
The values for
instance variables are provided by each instance of the class.
3.
Instances of the
same class share the same instance methods, implementations which reside in the
class.
Characteristics of class variables and
methods:
1.
All instances of
a class share its class variables and methods.
It is not necessary to have objects of a class
to use its variables and methods.
Abstraction:
Abstraction is a process
of identifying the relevant qualities and behaviors an object should possess. When we want to develop an application in OOP,
how do we look at it? How can I identity the data not merely in terms of
integer, string etc but at higher level like account, Employee etc. These could
be my variables, which are user defined and which OOP languages permit. To what
level I can look at these types of data and what these variables can do is what
we can call abstraction.
For example a given object has many features
but we may not be interested in all of them. Abstraction is concentrating on
these features that are essential and relevant and ignoring the rest. They
are two kinds of abstraction.
§
Data Abstraction
§
Functional
abstraction
Example Car
Data Abstraction: Engine no, speed etc.
Functional Abstraction: Start, stop,
accelerate etc.
Encapsulation:
In OOP data and methods are bound together.
Variables represent the state of an object and methods its behavior. Encapsulation
is the binding of behavior and state to a particular object. It embodies
information hiding.
|
|
It hides the non – essential details of an
object and shows only essential details.
Encapsulation promotes reuse of objects that
perform certain well defined tasks. Code can be used as a black box technology.
Messages
How do objects communicate with each other? If
I wish to start a car then I have to send a message start to the car object.
This is called sending a message. Software objects interact and communicate
with each other by sending messages to each other. For each message there
must be a designed server.
The components of a message are
i.
To whom the
message is addressed
ii.
The name of the
message (start, stop etc)
iii.
Any additional
information(Example: move at 40kmph)
The additional information necessary for
performing a task is sent as parameters or arguments.
Benefits of
messages:
Message passing supports all possible
interactions between objects.
Objects that send and receive messages need
not have to be in the same process or even in same machine.
Polymorphism:
Polymorphism is
a characteristic of being able to assign a different meaning or usage to
something in different contexts - specifically, to allow an entity such as a
variable, a function, or an object to have more than one form. A message
can perform or behave differently for different objects. Example: With the same
message ‘move’ the man walks, fish swim and birds fly. Again a message draw can
be used to draw lines; rectangles etc. This capability of a method to react
differently on different objects is called Polymorphism. This is possible
because the binding of the method with the message to an object takes place at
runtime. It is called late binding. Polymorphism helps in reducing the number
of functions to be remembered. Therefore code maintenance is easier.
Polymorphism is also known as
substitutability.
Inheritance:
Inheritance is the ability to define
a new class or object that inherits the behavior and its functionality of an
existing class. The new class or object is called a child or subclass or
derived class while the original class is called parent or base class.
In real life you can observe inheritance
almost everywhere. Child for example takes on the characteristics of the
parents. It is so in OOP also. This is one of the most powerful features of
OOP. Inheritance is the mechanism through which a class can be defined in terms
of an existing class.
Inheritance enables the refinement or
specialization of an existing class. Through inheritance a child can acquire
characteristics of its parents and can also have its own features. In the above
diagram the parent Venus flytrap not only inherits the characteristics of
plants also has features by which it traps and eats insects. Through inheritance
1.
Members can be
redefined
2.
Members can be
hidden
3.
More members can
be added
In OOP there is a super class from which all
classes inherit attributes. Usually these super classes are abstract i.e., they
do not exist in reality. There is no such thing existing in real life called
animal, but there are millions of animals like horses, dogs, and snakes etc. which
are actually of subclasses of animal’s class. The class animal exists only for
the purpose of a definition.
The class that inherits a set of attributes is
called the subclass and the class from which it inherits is called the super
class.
Benefits of
inheritance:
·
Subclasses
provide specialized behaviors from the basis of the common elements provided by
super class.
·
Through
inheritance, programmers can reuse code of the super class.
·
Programmers can implement
super classes called abstract classes that define generic behavior.
Multiple
inheritance:
There are many cases where a class can inherit
from two or more classes. A child inherits qualities from both the parents. A
class inheriting attributes from two or more classes is said to be showing
multiple inheritance.
In this diagram the company innovative
courier is both and international courier company as well as an insurance
company. It is in both business lines and is an example of multiple inheritance.
Java does not support multiple
inheritance as it has draw backs such as
· Inheritance of undesirable qualities also.
· Complexity
· Difficulty in maintaining and keeping track of
the code.
Code Reusability:
Code Reusability is reusing
the classes, packages, or interfaces that are already developed without
changing them but adding new features to it. This is implemented with the help
of inheritance. Code Reusability gives the following advantages to users
It helps in reducing the
code size since classes can be just derived from existing ones and one need to
add only the new features and it helps users to save their time.
Relationship between classes
There are three common relationships
between classes.
1.
Use
2.
Containment (“
has a”)
3.
Inheritance ( “
is a”)
The ‘use’ relationship is
obvious and general. For example there is a class called Employee and another
class Pay Details. It is natural that the Pay Details class use the Employee
class to generate a pay_slip. Therefore a class A uses B if
1)
a method of A
calls an object of B
2)
a method of A creates ,receives or returns object of B.
The ‘containment’ relationship
as the name suggests means that an object of a class can contain objects of another
class. For example an object of class A contains objects of class B. In such a
case objects of A should have at least one method that uses the objects of B. Otherwise
there wouldn’t be a necessity for objects of A to contain objects of B.
The ‘inheritance’ relationship
is an important aspect of OOP. A class A can inherit the properties of a class
B. This is dealt with in detail in the next chapter.
Guidelines for design of classes
1.Always keep data private.
2.Always initialize data.
3.Don’t use too many basic types in a class.
4.Breakup
classes with too many responsibilities
Name your classes, methods and data
corresponding to their responsibilities.
Advantages of OOP
·
Modularity: All classes and objects thereof can be
treated as separate modules. This makes designing simpler. A given system can
be very naturally and intuitively decomposed into modules using the class
concept.
·
Ease of
maintenance: Code is easier
to maintain. The concepts of Encapsulation localize the errors. If a car
doesn’t move, then we know there is something wrong with it and not with other
objects. So there no need to modify rest of the code.
·
Reusability
and extendibility: Through
inheritance we can easily extend existing classes. We can alter the behavior
and also add new features. As an example a car can also be used to pull another
vehicle. Code reusability is a great benefit.
·
Powerful
modeling paradigm: The
software system based on OOP is close to real world and hence simple to
understand.
Reference Books:
1. Grady Booch
2. Bala Guruswamy
Reference Books:
1. Grady Booch
2. Bala Guruswamy
No comments:
Post a Comment