CS F213
Object Oriented Programming
Prof.R.Gururaj
BITS Pilani CS&IS Dept.
Hyderabad Campus
Ch.2 of R1.
The Complete Reference- Java, 7th Edition, Herbert
Schildt, Tata McGraw Hill Publishing.
Ch.2 of R2. Object Oriented Analysis and Design with
Applications, Grady Booch, Addison Wesley, 2nd Edition.
And also refer to Class notes.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
The Object Model
Object-oriented Technology is built upon the sound
engineering foundation whose elements are
collectively known as Object Model.
The Object model encompasses the principles of :
Abstraction
Encapsulation
Modularity Major
Hierarchy
Typing
Concurrency Minor
And Persistence
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
All these concepts put together makes the object
model.
This objet-orientation requires a different thinking
about decomposition.
Object-oriented programming means different things
from structured programming.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
In 1980s it is predicted that:
Everyone will be in favor of OO.
Every manufacturer promotes his product as
supporting OO.
Every manager pays lip service to OO.
Every programmer practices OOP in a different
way.
But very few understood what it is.
Now we try to understand what is OO development
and what is not.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Object Model
Structured Design / Process-oriented programming
methods evolved to guide developers who were
trying to build complex systems using algorithms
as their fundamental building blocks.
Object-oriented Design methods evolved to help
developers exploit the expressiveness of object-
oriented programming languages which use the
class and object as the basic building block.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Object-oriented Software
Development
OOA, OOD, and OOP are related and are used in
developing object-oriented software.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
What is OOA
Object-oriented Analysis refers to a method of
analyst that examines the requirements from
the perspective of classes and objects found in
the vocabulary of problem domain.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
What is OOD
Object-oriented Design refers to any design
methodology that leads to object oriented
decomposition.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
What is OOP
Object-oriented programming is a method of
implementation in which programs are organized as
cooperative collections of objects , each of which
represents an instance of some class.
Characteristics of Object-oriented Programming:
1. Has Objects, not algorithms, as its basic building
blocks
2. Each object is an instance of some class
3. Classes are related to each other via inheritance
relationship
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Object-oriented languages-
1. Support objects that are data abstractions with
an interface of named operations and hidden
local state.
2. Objects have an associated type [class].
3. Type may inherit attributes from supertypes
[super class]
Ex: Smalltalk, Ada, C++, Java, Eiffel, Object
Pascal etc.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Programming Paradigms
Paradigm Basis Suitability
Procedure-oriented Algorithms/functions Computation-intense operations
Logic-oriented Goals; predicate logic Knowledgebase systems
Rule-oriented rules Rule-based operations
Object-oriented Objects, classes Broadest set of applications;
Architectural frameworks
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Elements of OO Model
Major Elements:
1. Abstraction
2. Encapsulation
3. Modularity
4. Hierarchy
Minor Elements:
1. Typing
2. Concurrency
3. Persistence
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Abstraction
An essential element of object-oriented approach is abstraction.
Humans manage complexity through abstraction.
Using abstraction we can ignore the inner details which are not
so essential, and still we could use an object as a whole.
Ex: Car
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
An abstraction focusses on the outside view of an object, and so
serve to separate an objects essential behavior from its
implementation.
We focus on observable behavior.
The inside view focuses on how this contract is carried out by the
object (often in collaboration with other objects).
Abstraction can be seen as a simplified description of a system
that emphasizes some of the system details / properties while
suppressing others.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Client is an object that uses the resources of another object
known as server.
We may characterize the behavior of an object by considering the
services that it provides to other objects, as well as the operations
that it may perform on other objects.
A good abstraction is one that emphasizes details that are
significant to the reader or user and suppressing details that are
immaterial for the moment.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Individually each operation has its own unique signature-
return type, name, formal arguments.
An operation of an object is also called as method or
member function.
No object stands alone; every object collaborates with
other objects to achieve some behavior.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Encapsulation
Encapsulation hides the details of implementation of an object.
How we implement, what data structures we use etc., are
immaterial to the user with respect to the contract with the
class.
Encapsulation binds data and code together. Code manipulates
data.
It provides a wrapper that prevents code and data from arbitrary
access. This access can be controlled through well defined
interfaces.
Encapsulation is achieved through class. We can hide the
complexity by assigning members with access specifiers.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
In encapsulation we focus upon the implementation that gives
rise to behavior.
Encapsulation is achieved through information hiding, which is
the process of hiding all secrets of an object, that do not
contribute to the essential characteristics.
Typically, we hide structure of an object, as well as the
implementation of methods.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Each class must have two parts:
Interface
Implementation
Interface of a class captures only the outside view encompassing
the behavior.
The implementation of a class comprises of the representation
of the abstraction as well as the mechanisms that achieve the
behavior.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Summary of Encapsulation
Encapsulation is the mechanism that binds data and code.
Keeps both safe from outside interference.
It works like a protective wrapper around the code and data ,
that prevents arbitrary access, by the code defined outside the
class.
Access to code and data contained in the wrapper is tightly
controlled by through well-defined interfaces.
In Java the basis for encapsulation is the CLASS.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Since the purpose of CLASS is to hide the complexity, there are
mechanisms for hiding members (complexity).
We have public, private, protected members.
Hence we need to design public interfaces carefully, not to
expose too much of the inner workings of a class.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Modularity
It is the act of partitioning a program into individual
components.
This is to reduce the complexity.
You have a number of well defined documented boundaries with
in the program.
What is the right set of modules for a problem?
It is as hard as deciding upon right abstraction.
Modules serve as physical containers in which we declare classes
and objects of our logical view.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
In structured design: it is concerned with the meaningful
grouping of subprograms.
In objet-oriented approach it grouping of classes and objects.
The overall goal is to:
Reduce SW cost by allowing modules to be designed and revised
independently.
The module structure should be simple enough to be
understood.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Since the modules usually serve as the elementary and
indivisible units of SW that can be reused across applications.
A developer may choose to package the classes and objects into
modules in such a way that makes their reuse convenient.
Other advantages:
1. Work assignment in Development team.
2. Interfacing between modules in minimum and well-defined.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Hierarchy
Abstraction facilitates outside view.
Encapsulation helps in managing the complexity by hiding the
inner details.
Modularity helps in clustering logically related abstractions.
A set of abstractions often forms hierarchy, and by identifying
these hierarchies in our design, we generally simplify the
understanding of the problem.
Hierarchy is a ranking or ordering of abstractions.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Inheritance is the most important is a hierarchy.
It is an essential element of object oriented system.
Inheritance is the process by which one object acquires the
properties of another object.
This supports hierarchical classification.
Basically inheritance defines relationship between classes,
where in one class shares the structure and behavior of one or
more other classes.
Thus we have single inheritance and multiple inheritance.
Thus inheritance represents a hierarchy of abstractions.
Semantically, inheritance denotes is-a kind of relationship.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Inheritance thus implies generalization and specialization
hierarchy.
A subclass specializes the more general structure or behavior of
the superclass.
If A is not kind of B the A should not inherit B
As we evolve our inheritance hierarchy the structure and
behavior that are common to different classes will tend to
common superclasses.
Superclass represents generalized abstractions.
Subclasses represent specialization of superclasses.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Multiple Inheritance is conceptually straightforward, but induces
some practical complexities for programming languages.
We will have to address two things:
Name clashes.
Repeated inheritance.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Name clashes:
Ex:
C++ resolves the name clash by attaching the classname
qualifier.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Repeated inheritance:
This occurs when two or more peer superclasses share a
common superclass.
In such situation, the inheritance lattice will be a diamond, and
so the question arises, does the leaf class have one copy or
multiple copies of the structure of the shared superclass.
Ex:
Most of the cases, multiple inheritance is misused.
Hence it is prohibited by some languages.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
The higher-level abstraction is generalized.
The lower level abstraction is specialized.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Another form of hierarchy is part of relationship among the
objects. This is also known as Aggregation concept.
This permits physical grouping of logically related structures.
Aggregation is a kind of association where object of Class A is
made up of objects of class B.
This suggests whole part relationship between A and B.
Composite aggregation or simply composition, implies that each
instance of the part belongs to only one instance of the whole,
and that the part cannot exist except as part of the whole.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Typing
A type is a precise characterization of structural or behavioral
properties of a collection of entities all share.
Typing is the enforcement of the class of an object, such that
objects of different types may not be interchanged, or at the
most they may be interchanged only in very restricted ways.
A programming language may be strongly typed, and
some are untyped.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Ex:
Java , C++ , Ada are strongly typed.
Violation of type conformance can be detected at compile time.
Smalltalk is untyped.
Any object can invoke some method on any object, even
that method is not defined for that object. Violation is not
known until execution.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Advantages of strongly typed languages
1. Without this type checking programs can crash in
mysterious ways.
2. Early detection of bugs makes edit-compile-debug cycle
effective.
3. Type declaration helps to document programs.
4. Most compilers generate efficient code if types are
declared.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Static and Dynamic Binding:
This concept is different from strong/untyped.
Binding :refers to the time when names are bound to object
types.
Static/early binding : Means that the type of object pointed
by reference is fixed at the time of compilation.
Dynamic binding: Object types are not known until runtime.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Ada - Strongly typed and static binding.
Smalltalk- untyped and dynamic.
Java, C++ - strongly typed and support dynamic binding.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
class TypeDemo
{
public static void main(String args[])
{
Shape s;
s=new Box(); //illegal
}
}
class Shape
{
}
class Rectangle extends Shape
{
}
class Box
{
}
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Polymorphism:
Represents a concept in type theory in which, single name
(variable) may denote objects of many different types, that
are related by some common super class.
Poly morphism exists when features of inheritance and
dynamic binding interact.
This is the most powerful feature of Object orientation.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Concurrency
For certain kinds of problems, an automated system may have to
handle many different events simultaneously.
Threads.
Single CPU, multiple CPUs.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Persistence
An object in software takes up some amount of space and exists
for a particular amount of time.
Persistence is the property of an object through which its
existence transcends time ( i.e. the object continues to exist after
its creator ceases to exist), and/or space ( i.e. the object moves
form one address space to another),
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Object Model
Object model does not abandon the sound principles of process-
oriented model.
Rather it introduces several novel elements that build upon
these earlier models.
The use of Object model helps us to construct well-structured
complex systems.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Benefits of Object Model
1. Object model helps us to exploit the expressive power of
object based languages.
2. Reuse of SW and Design both, leading to SW frameworks.
3. Systems are more resilient to change.
4. Develop more complex systems with confidence and
correctness.
5. Humans find the idea of object orientation quite natural.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Summary
Principles of Object Model
OOA/OOD/OOP
Abstraction
Encapsulation
Modularity
Hierarchy
Typing
Concurrency and Persistence
Benefits of Object model
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus