Ch. Ravi Singh , RBCET BLY, CS Dept.
Objected Oriented Programming
One characteristic that is constant in software industry today is the “change”. Change is
one of the most critical aspects of software development and management. New tools
and new approaches are announced almost every day. The impact of these
developments is often very extensive and raises a number of issues that must be
addressed by the software engineers. Most important among them are maintainability,
reusability, portability, security, integrity, and user friendliness of software products.
To build today’s complex software it is just not enough to put together a sequence of
programming statements and sets of procedure and modules. We need to use some
programming techniques that are easy to implement and modify in a wide variety of
situations. These include techniques such as modular programming, top-down
programming, bottom-up programming and structured programming.
Computer languages do not exist in a void. Rather they relate to one another, with each new
language influenced in one form or another by the ones that came before. In a process akin to
cross- pollination, features from one language are adapted by another, a new innovation is
integrated into an existing context, or an older construct is removed. In this way languages
evolve and the art of programming advances. JAVA is no exception.
C: The Beginning of the Modern Age of Programming:
The creation of C marks the beginning of the modern age of programming. C was invented
by Dennis Ritchie in the 1970s on a DEC PDP -11 that used the UNIX operating system.
While some earlier languages, most notable Pascal, had achieved significant success, it was C
that established the paradigm that still charts the course of programming today.
C was structured programming language. It has well – defined control statements, subroutines
with local variables, and some other improvements. Through the use of structured techniques
program became better organized, more reliable, and easier to manage.
The C language had its limits. One of the most troublesome was its inability to handle large
programs. The C language hits a barrier once a project reaches a certain size, and after that
point, C programs are difficult to understand and maintain. So with C there is always a
threshold beyond which a C program becomes unmanageable.
The Creation of OOP and C++:
By the late 1970s, the size of many projects was near or at the limits of what structured
programming methodologies and the C language could handle. To solve this problem, a new
way to program began to emerge. This method is called Object Oriented Programming
(OOP).Using OOP; a programmer could handle much larger programs. The trouble was that
C, the most popular language at that time, did not support object oriented programming. The
desire for an object oriented version of C ultimately led to the creation of C++.
C++ was invented by Bjarne Stroustrup beginning in 1979 at the Bell Laboratories in Murray
Hill, New Jersey. He initially called the new language “C with Classes”. However, in 1983
the name changed to C++. C++ contains the entire C language. Thus, c is the foundation upon
which C++ is built. Most of the additions that Stroustrup made to C were designed to support
object oriented programming.
Now, it is critical to understand that the invention of C++ was not an attempt to create an
entirely new programming language. Instead, it was an enhancement to an already highly
successful language. This approach to language development- beginning with an existing
language and moving it forward- established a trend that continues today.
“C++ is basically a procedural language with object- oriented extension”.
“The next major advance in programming languages is Java. Work on Java, which was
originally called Oak, began in 1991 at Sun Microsystems. The main driving force
behind Java design was James Gosling. Patrick Naughton, Chris Warth, Ed Frank, and
Mike Sheridan also played a role.”
“Java is pure object – oriented language”.
Object Oriented Paradigm:
Object Oriented Programming is an approach to program organization and development.
Object –Oriented Programming took the best ideas of structured programming and combined
them with several new concepts. The result was a different and better way of organizing a
program. In the most general sense, a program can be organized in one of two ways: around
its code (structured programming) or around its data (OOP). The major objective of object –
oriented approach is to eliminate some of the flaws encountered in the procedural approach.
OOP treats data as a critical element in the program development and does not allow it to
flow freely around the system (program). It ties data more closely to the functions that
operate on it and protects it from unintentional modifications by the other functions. OOP
allows us to decompose a problem into a number of entities called Objects and then build
data and functions (known as method in Java) around these entities. The combination of
data and methods make up an object.
Method Method
Data
Method Method
Object= Data+ Method
The data of an object can be accessed only by the methods associated with the object.
However, methods of an object can access the methods of other objects.
Definition of Object-Oriented Programming:
“Object-oriented programming (OOP) is a programming paradigm that represents
concepts as "objects" that have data fields (attributes that describe the object) and
associated procedures known as methods”. Objective-C, Smalltalk, Java and C# are
examples of object-oriented programming languages.
“Object Oriented Programming is an approach that provides a way of modularizing
programs by creating partitioned memory areas for both data and functions that can be
used as templates for creating copies of such modules on demand.”
This means that an object is considered to be a partitioned area of computer memory that
stores data and a set of operations that can access the data. Since the memories partitioned are
independent, the objects can be used in a variety of different programs without modifications.
Some of the features of Object Oriented Programming are:
1. Emphasis on data rather than procedure.
2. Programs are divided into what are known as objects.
3. Data structures are designed such that they characterize the objects.
4. Methods that operate on data of an object are tied together in the data structure.
5. Data is hidden and cannot be accessed by external functions.
6. Objects may communicate with each other through methods.
7. New data and methods can be easily added whenever necessary.
8. Follows bottom- up approach in program design.
Objects and Classes:
Objects are the basic runtime entities in an object oriented system. They may
represent a person, a place, a bank account, a table of data or any item that the program
may handle. They may also represent user defined data types such as vectors and lists.
When a program executed, the objects interact by sending message to one another. For
example ‘customer’ and ‘account’ are two objects in a banking program, then the
customer object may send a message to the account object requesting for the balance.
Each object contains data and code to manipulate the data. Objects can interact without
having to know the details of each other’s data or code.
“Objects contain data and code to manipulate that data”.
The entire set of data and code of an object can be made a user defined data type using
the concept of class. “A class may be thought of as a ‘data type’ and an object as a
‘variable’ of that data type.” Once a class has been defined, we can create any number
of objects belonging to that class. Each object is associated with the data of type class
with which they are created. So we can say
“A class is a collection of objects of similar types.” For example, mango, apple and
orange are members of the class fruit. Classes are user defined data types. If fruit
has been defined as a class the statement Fruit mango* will create an object
belonging to the class fruit. Or
“In object-oriented programming, a class is a template definition of the methods and
variables in a particular kind of object. Thus, an object is a specific instance of a
class.”
Person
Name
Basic Pay
Salary()
tax()
Representation of an object.
Data is name and Basic Pay, Methods are Salary ( ) and tax ( ), Object is person.
Characteristics Required by OO Programming Approach:
Object Orientation means organization of software as a collection of discrete objects. Each
object consists of data structure (Attributes) and behavior (Operations) which are closely
coupled. Some basic characteristics required by OO programming Approach are:
1. Identity: The identity means data is arranged in distinguishable entities called
objects. Each object is treated as an inherent entity that means- each object is distinct even if
their attribute values are same.
“An object identity is property by which each object can be identified and treated as a
distinct software entity.”
Example, Ram’s Pen, Seta’s ball,
2. Classification: Classification is a technique in which objects of same data structure
(Attributes) and behavior (Operations) are grouped into classes. Each object can have its
own values for attributes and operations but these objects can share the attributes and
operations of other objects too. Shape can be a class and rectangle, triangle and square can be
the objects of the class Shape. The following figure illustrates this idea:
Shape Objects
Shape class
Attributes
Length
Breadth
Operations
Compute _area
Compute_ perimeter
3. Data Abstraction and Encapsulation: The wrapping up of data and methods into a
single unit (called class) is known as encapsulation. Data encapsulation is the most striking
feature of a class. The data is not accessible to the outside world and only those methods,
which are wrapped in the class, can access it. The insulation of the data from direct access
by the program is called data hiding. Encapsulation makes it possible for objects to be
treated like “black boxes”, each performing a specific task without any concern for internal
implementation.
“Object oriented encapsulation is a mechanism in which operations and attributes are
packaged together”. Encapsulation Enforces Modularity.
Information in Information out
Data and method
Abstraction refers to the act of representing essential features without including the
back ground details or explanations. Due to this the observer has the knowledge about-
what the object can do but has no knowledge of how it does.
Example of abstraction is ticket reservation system.
4. Inheritance: Inheritance is the process by which objects of one class acquire the
properties of objects of another class. Inheritance supports the concept of hierarchical
classification. For example, the bird robin is a part of the class flying bird, which is again a
part of the class bird. As shown in figure given below, the principle behind this sort of
division is that each derived class share common characteristics with the class from which it
is derived. Inheritance Passes "Knowledge" Down.
Bird
Attributes:
Feathers
Lay Eggs
Flying Bird Nonflying
Attributes: Bird
Attributes:
Robin
Swallow
Penguin Kiwi
Attributes:
Attributes:
Attributes: Attributes:
In OOP, the concept of inheritance provides the idea of reusability. This means we can add
additional features to an existing class without modifying it. This is possible by deriving a
new class from the existing one. In Java the derived class is known as ‘subclass’.
5. Polymorphism: Polymorphism is another important OOP concept. Polymorphism
means the ability to take more than one form. For example, an operation may exhibit
different behavior in different instances. The behavior depends upon the types of data used in
the operation. For example, consider the operation of addition. For two numbers, the
operation will generate a sum. If the operands are strings, then the operation would produce a
third string by concatenation. Figure illustrates that a single function name can be used to
handle different number and different types of arguments. This is similar to a particular word
having different meanings depending on the context.
Polymorphism is extensively used in implementing inheritance.
Polymorphism Takes any Shape.
Shape
Draw ( )
Circle Object Box object Triangle Object
Draw (circle) Draw (box) Draw (triangle)
Polymorphism
6. Dynamic Binding: A mechanism called dynamic binding is needed to implement
polymorphism in object-oriented systems. Dynamic binding is a property of OO
Programming that allows a class to select required method at run time.
In this example the polymorphic algorithm invokes a method F() that is one of the common
properties shared among a set of classes. The algorithm is unaware of the exact type of the
object to which the invocation is directed. In the example, it is not known whether the object
is of class X or class Y. The dynamic binding mechanism determines the type of the object
and maps the invocation to the correct method in class X, if the object is of class X, or to the
correct method in class Y, if the object is of class Y.
“Dynamic Binding means that the code associated with a given procedure call is not
known until the time of the call at run time”.
7. Overriding: The Overriding is redefinition of method defined in one class in one of
the subclass.
For example, in above figure method draw ( ) is redefined in three subclasses.
8. Overloading is a mechanism in which we use many methods have same name but can
pass different number of parameters or different types of parameters.
For example,
Sum (int a, int b, int c);
Sum (float x, float y);
Generosity:
Generosity is a property in object oriented technology by which the container class can
be created in which the implementation of any data type is contained. These container
classes are popularly known as templates.
Example, Consider the implementation of data structure stack. If we create an integer stack
“stack of integers” then the push and pop operations will handle only integer elements. If we
create a character stack “stack of characters” then the push and pop operations will handle
only character elements. But creating so many copies of implementations makes the code
complex to maintain. Hence the principal of generosity is used and a template class (for
the above example template class is “stack of anything”) can be created. This container
class can handle any data type element at run time.
In the given figure below, The two parameters of the generic class represent the specific
kind of "key", in this case a person’s name, and the specific kind of "value", in this case a
phone number. This combination of parameters yields a Phonebook. Other combinations of
keys and values would yield other useful forms of Lookup Tables.
Modeling:
Building software is similar to build a house. One should gather all the necessary needs and
wants before constructing the house. The blueprint of house must be prepared before the
actual construction work. Just similar to that before creating software it is necessary to collect
all possible requirements. The model for the software must be prepared on the paper.
“Modeling is a well-accepted engineering technique for designing the software. The
architectural and mathematical models can be build for creating the software”.
“Modeling is an activity the aim of which is to make a particular part or feature of the
software easier to understand, define, quantify, visualize, or simulate”.
Principles of Modeling:
1. The choice of model has a profound influence on how a problem is attacked and how
the solution is shaped.
2. Every model may be expressed at different levels of abstraction.
3. The effective models are connected to reality.
4. No single model is sufficient. Nontrivial systems are best described a set of
independent but related models.
Model:
A model provides the blueprint of the system. A good model includes those elements that
have broad effect and omit those minor elements that are not relevant to a given level of
abstraction.
Aims of modeling:
1. We model the system because we want to understand the system which we are
building.
2. Models help to visualize the system.
3. Models allow us to specify the structure or behavior of the system.
4. Models help in documenting the decisions that are made during the development of
the software.
5. Using model we get the template, using which the system can be constructed.
Object- Oriented Modeling:
Generally while building software systems there are two commonly used approaches of
modeling, 1. Algorithmic Perspective 2. Object –Oriented Perspective.
The building block of algorithmic perspective is function or procedure. In this approach the
larger algorithm is decomposed into smaller ones. As the requirements get changed or as the
system grows, it becomes very difficult to maintain the algorithm.
The modern software development approach is based on Object- oriented Perspective.
The main building block of this approach is object or class.
Importance of OOP over Conventional Approach:
1. The conventional programming executes series of procedures sequentially. In object
oriented programming approach there is a collection of objects.
2. Conventional programming is top-down programming approach. OOP is bottom-up
programming approach.
3. In Conventional programming major focus is on procedures or functions. In OOP
major focus is on objects.
4. In conventional programming data reusability is not possible. In OOP data reusability
is possible.
5. In conventional programming data hiding is not possible. In OOP data hiding is
possible.
6. Conventional programming is simple to implement. OOP is complex to implement.
7.
Benefits of OOP Approach:
The main advantages of OOP are:
simplicity: software objects model real world objects, so the complexity is reduced
and the program structure is very clear;
modularity: each object forms a separate entity whose internal workings are
decoupled from other parts of the system;
Modifiability: it is easy to make minor changes in the data representation or the
procedures in an OO program. Changes inside a class do not affect any other part of a
program, since the only public interface that the external world has to a class is
through the use of methods;
extensibility: adding new features or responding to changing operating environments
can be solved by introducing a few new objects and modifying some existing ones;
maintainability: objects can be maintained separately, making locating and fixing
problems easier;
Re-usability: objects can be reused in different programs.
Data hiding can be done using OOP.
In OOP everything is an object. The communication among different objects is via
message passing mechanism.
Message passing mechanism
Applications of OOP:
The areas for applications of OOP include:
1. Real -Time system.
2. Object- Oriented database.
3. AI and expert systems.
4. Hypertext, Hypermedia, Expertext.
5. CAM/CAD systems.
6. Neural networks and parallel programming.
UML: The Unified Modeling Language had originally been developed to create models for
software systems. The UML is a general purpose visual modeling language that is used to
specify, visualize, construct, and document the artifacts of a software system.
“UML is a language for visualizing, specifying, constructing, and documenting the
artifacts of a software system. The examples of artifacts are requirements, architecture,
design, source code, test cases and prototypes etc.”
Visualizing: UML contains a collection of graphical symbols. For each symbol there is a
well defined semantic.
Specification: UML help in building the models that are precise, unambiguous and complete.
Specification is used to understand the complete behavior of the system. It is also used for
understanding functional and non functional requirements.
Construction: UML is not simply the graphical tools but one can map the model directly to
the programming language. The process of mapping from model to programming language is
called forward engineering whereas mapping back from implementation to UML model is
called reverse engineering.
Documentation: Software development is done using following stages: Requirement
gathering and analysis, Specification, Design, Coding, Testing, Maintenance. UML help in
documentation of all these stages.
Strong Point of UML:
1. This modeling is independent of the programming language.
2. UML is suitable for modeling various kinds of systems such as enterprise information
systems, distributed web based applications, real time embedded systems etc.
3. UML is easy to use for modeling.
4. UML supports high level concepts such as collaboration, framework, patterns and
components.
5. Provide extensibility mechanisms to extend core concepts.
Week Point of UML:
1. It takes time to keep the software design reasonable and synchronized.
2. It is not possible to represent all the possible details of the software implementation in
the software design model.
The Conceptual Model of UML:
The conceptual model of UML has three elements and those are
1. Basic building blocks
2. Rules of UML
3. Common mechanisms in UML
Basic Building Blocks:
The basic building blocks include things, relationships and diagrams.
Things: Things are the basic and abstract entities in model. There are four types of things
1. Structural Things: These are static part of a model, representing elements that are
either physical or conceptual. The structural things are also called as classifier. These
are basically nouns.
Examples: Class, Object, Component, Interfaces, Node, Collaboration etc.
2. Behavioral Things: These are dynamic part of a model. These are verbs of the
model representing behavior of the system. There are three types of behavioral things:
Interaction: Interaction represents the set of messages exchanged among the set of
objects within particular context to accomplish certain task. It is represented by a
directed line.
Display
State Machine: The state machine shows the life cycle of the object. This represents
the events and states of object and behavior of the object in reaction to event. It is
represented by a rectangle with rounded corners.
Reading Card
Activity: Activity is the behavior that specifies the steps performed by the
process. Activity is represented by a rectangle with rounded corners.
Initializing
3. Grouping Things: Grouping things are the organizational parts of UML models.
These are the boxes into which a model can be decomposed. The primary kind of
grouping is called package. A package can be represented as follows:
Technical services
4. An Notational things: These are for explanation purpose. The most commonly used
annotational thing is note. It can be represented as a rectangle with dog – eared
corner.
This note provides additional
information about the diagrams.
Relationship in UML: There are four types of relationship in UML
1. Dependency: This kind of relationship exits when one element is dependent on
another element. Dependency is a type of relationship in which one element uses the
information and services of another element.
Example: Line………………………….Ponit
Database……………………… Connection
Dependency is represented by dashed line possibly directed and having label.
2. Generalization: This shows the parent child relationship. Child shares the behavior
of the parent. Graphically it is represented by a solid line with a arrow head at one
end.
Example:
Account
Saving Current
3. Realization: It is relationship between two model elements, in which one model
element realizes the behavior that the other model element specifies. It is represented
by dashed line possibly directed and having label.
………………………………….
Example:
4. Association: This is the simple relationship to link two objects. It is a relationship in
which one object is get connected to the other object. It is represented by solid line
possibly directed and having label. Association is a relationship between two
objects. In other words, association defines the multiplicity between objects. You
may be aware of one-to-one, one-to-many, many-to-one, many-to-many all these
words define an association between objects
Has
Customer Account
Aggregation: Aggregation is a kind of association used for representing whole part
relationship. In this case, one class represents the larger entity which consists smaller
things. It is represented as a solid line having unfilled diamond at the larger entity
end.
Example: Car DVD Player
Composition: The aggregation and composition are whole part relationship.
The composition is restricted aggregation. That is, when a object contains
another object and if contained object cannot exist without the existence of the
container object, then it is called composition.
Example: Steering and Car. A car cannot exist without a steering.
Steering Car
Diagrams: Diagrams are the graphically representation of the set of the elements. Various
types of diagrams that can be drawn in UML are:
Object diagram, Class diagram, Component diagram, sequence diagram, State
diagram, Activity diagram, Package diagram, timing diagram etc.
Rules of UML:
The UML rules are defined for names, scope, visibility, integrity and execution etc.
A name represents the things, relationships, diagrams. The scope represents the some text that
gives a specific meaning to the names. The visibility shows the names that can be seen and
used by others. The integrity shows how properly and consistently the entities can be related
to one another. The execution represents the running or the implementation of the model.
Common Mechanism in UML:
There are four common mechanism used in UML:
1. Specification: Along with graphics the UML provides textual statements that have
some special semantic and syntax. For Example along with class, one can specify
the attributes and operations carried out by that class.
2. Adornments: These are graphical notations of UML, specify the things in more
details. This helps in proper visualization of a system. For Example along with
rectangular graphical representation, we can specify the attributes and
operations of a class in details.
3. Common division: In UML there is common division between some concepts:
a. Classes and Objects: Class is an abstraction of object and object is an instance of
a class. For example a class can be Employee and objects can be Sachin, Ram or
any other employee.
Employee
Sachin: Employee:
Employee id
Class Object
b. Interfaces and Implementations: The interface represents a contract between
two entities, whereas the implementation represents the concrete realization of the
contract. The interface’s complete semantic can be defined by the implementation.
I Unknown Sale
Amount I Repository
I Payment
Interfaces and Implementations
c. Roles and Types: The type represents the class of an entity such as object,
attribute, and parameters and so on. The role defines the meaning of an entity
with respect to a class. One entity can be derived from the other entity and the
entity which gets derived takes some of the characteristics from its parent along
with its own characteristics in the given context.
Course
Student:
Student:Person
Person
Role is specifies at the end of the relation.
Roles
Student Assigned Student Assigned Course Course
4. Extensibility: Sometimes it is not possible to represent all the refinements of the system
using the model. For this reason, the UML is opened- ended, making it possible for you to
extend the language in controlled ways. The UML extensibility mechanism includes:
a. Stereotypes b. Tagged values c. Constraints.
Multiplicity: Multiplicity means how many objects are connected.
Notations Descriptions
1 Only one instance
0..1 Zero or one instance
* Many instances
0..* Zero or many instances
1…* One or many instances
2…10 You can specify the integer range
Notations that are used to describe the multiplicity
Teacher 1…* Teaches 0…* Subjects
Architectural Model of UML: The architecture is a collection of significant decisions
about, the organization of software system, the collection of structural elements and their
interfaces, the behavior of the elements with the collaborative objects.
The architecture of a software system can be described by five types of views:
Figure
Design View: This view supports the functional requirements of the system. It represents the
services that are provided to the end user by the software system.
With UML the static aspects of a system are represented by the class diagram and the object
diagram, whereas the dynamic aspects are represented by the interaction diagram, state
diagram and the action diagrams.
Interaction View: This view focuses on the flow of control among the various parts of the
system. This view focuses on performance, scalability and throughput of the system.
Use case View: This view supports all the activities that are required to deploy all the
hardware technology required to execute the system in the target environment. The focus of
this view is on distribution, delivery and installation of various parts in order to make up the
whole system.
Deployment View: Deployment view means all the activity required to build a software
system are ready to use.
Implementation View: This view contains a collection of artifacts which are combined and
assembled together to form the physical system.
NOTE:
Difference between aggregation and composition
Composition is more restrictive. When there is a composition between two objects, the
composed object cannot exist without the other object. This restriction is not there in
aggregation. Though one object can contain the other object, there is no condition that the
composed object must exist. The existence of the composed object is entirely optional. In
both aggregation and composition, direction is must. The direction specifies which object
contains the other object. Example: A Library contains students and books. Relationship
between library and student is aggregation. Relationship between library and book is
composition. A student can exist without a library and therefore it is aggregation. A
book cannot exist without a library and therefore it’s a composition.
Another Example: