Object Oriented Programming (Using Java)
Object Oriented Programming (Using Java)
Chapter 1
1. Introduction
With the advent of languages such as C, structured programming became very popular and was
the paradigm of the 1980s. Structured programming proved to be a powerful tool that enabled
programmers to write moderately complex programs fairly easily. However, as the program grew
larger, even the structured approach failed to show the desired results in terms of bug –free, easy-
to-maintain and reusable programs.
Object-Oriented Programming (OOP) is an approach to program organization and development,
which attempts to eliminate some of the pitfalls of conventional programming methods by
incorporating the best of structured programming features with several new concepts. It is a new
of organizing and developing programs and has nothing to do with any particular. However, not
all languages are suitable to implement the OOP concepts easily. Languages that support OOP
features include Smalltalk, Objective C, C++, Ada and Object Pascal. C++, an extension of C
language, is the most popular OOP today. C++ is basically a procedural language with object-
oriented extension. The last one added to this list is Java, which is a pure object-oriented
language.
Method Method
Dat
a
1
Method + Methods
Fig 1.1 Object=data Method
The data of an object can be accessed only by the methods associated with that object.
However, methods of one object can access the methods of other objects.
Definition:
OOP: - is an approach that provides a way of modularizing programs by creating partitioned
memory area for both data and methods 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 that data. Since the memory partitions
are independent, the objects can be used in a variety of different programs without modification.
Object
An object is a core concept, and is a model or representation of real-world entity or logical
idea or concept. It may represent a person, a place, a bank account, a able of data or any item
that the program may handle. It may also represent user-defined data types such as vectors
and lists. As pointed out earlier, an object takes up space in the memory and has an
associated address like a structure in C.
When a program is executed, the objects interact by sending messages to one another. For
example lets say ‘customer’ and ‘account’ are two objects in 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. It is sufficient to know the type of message
accepted and the type of response returned by the objects.
Class
We just mentioned that 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 concept of a
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
2
created. A class is thus a collection of objects of similar type. For Example mango, apple
and orange are members of the class fruit.
Definition: A class is a template used to create multiple objects with similar features.
Person Object
Name
Data
BasicSalary
Salary ()
Methods
Tax()
Classes are user-defined data types and behave like the built-in data types of a programming
language. For example , the syntax used to create an object is no different than the syntax
used to create an integer object in C++. If Fruit has been defined as a class, then the
statement
Fruit mango;
will create an object mango belonging to the class fruit.
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. In OOP,
the concept of inheritance provides the idea of reusability. This means that we can add
additional features to an existing class without modifying it. This is possible by deriving
a new class from an existing one. The new class will have the combined features of both
the classes. Thus the real appeal and power of the inheritance mechanism is that it allows
the programmer to reuse a class that is almost, but not exactly, what he wants, and to
tailor the class in such a way that it does not introduce any undesirable side effects into
the rest of the classes.
3
Polymorphism
Polymorphism means the ability to take more than one form. For example, an operation
may exhibit different behavior in different instances. The behavior depends on the type of
data used in the operation.
Shape
Draw ()
Dynamic Binding
It refers to the linking of a procedure call to the code to be executed in response to
the call. Dynamic binding means that the code associated with a give procedure call is not
known until the time of the call at run time. It is associated with polymorphism and
inheritance. A procedure call associated with a polymorphic reference depends on the
dynamic type of that reference.
Consider the procedure ‘draw’ in Fig 1.3 above. By inheritance, every object will
have this procedure. Its algorithm is, however, unique to each object and so the draw
procedure will be redefined in each class that defines the object. At run-time, the code
matching the object under current reference will be called.
Message Communication
An object-oriented program consists of a set of objects that communicate with each
other. The process of programming in object-oriented language, therefore, involves the
following three basic steps:
1. Creating classes that define objects and their behavior.
2. Creating objects from class definitions.
3. Establishing communication among objects.
Objects communicate with one another by sending and receiving information much
the same way as people pass messages to one another. A message for an object is a
4
request for execution of a procedure, and therefore will invoke a method (procedure)
in the receiving object that generates the desired result.
Note: - Objects have life-cycle. They can be created and destroyed. Communication with
an object is feasible as long as it is alive.