01chapter One-Introduction To OOP
01chapter One-Introduction To OOP
01chapter One-Introduction To OOP
Oriented Programming
Chapter- One: Introduction to Object-Oriented Programming
Assembly languages were developed to make programming easy. However, since the computer
cannot understand assembly language, a program called an assembler is used to convert
assembly-language programs into machine code, as shown in Figure 1.1:
FIGURE 1.2: A source program is compiled into a machine-language file, which is then
linked with the system library to form an executable file.
Operating Systems
The operating system (OS) is the most important program that runs on a computer, which
manages and controls a computer’s activities. The popular operating systems are Microsoft
Windows, Mac OS, and Linux. Application programs, such as a Web browser or a word
processor, cannot run without an operating system. The interrelationship of hardware,
operating system, application software, and the user is shown in Figure 1.3:
FIGURE 1.3 The operating system is the software that controls and manages the system.
The functional programming paradigm has its roots in mathematics and it is language
independent. The key principal of this paradigm is the execution of series of mathematical
functions. The central model for the abstraction is the functions which are meant for some
specific computation and not the data structure. Data are loosely coupled to functions. The
functions hide their implementation. Function can be replaced with their values without changing
the meaning of the program. Some of the languages like perl, javascript mostly uses this
paradigm.
This programming methodology is based on data and its movement. Program statements are
defined by data rather than hard-coding a series of steps. A database program is the heart of a
business information system and provides file creation, data entry, update, query and reporting
functions. There are several programming languages that are developed mostly for database
application. For example SQL. It is applied to streams of structured data, for filtering,
transforming, aggregating (such as computing statistics), or calling other programs. So it has its
own wide application.
Lack of Control: - In declarative programming, you may use functions that someone else
created, in order to achieve the desired results. But you may need specific things to be completed
behind the scenes to make your result come out properly. You do not have this control in
declarative programming as you would in imperative programming.
Inefficiency: - When the implementation is controlled by something else, you may have
problems making your code efficient. In applications where there may be a time constraint, you
will need to program the individual steps in order to make sure your program is running as
efficient as possible.
For example: To give directions in an imperative fashion, you say, “Go to 1st Street, turn left
onto Main, drive two blocks, turn right onto Maple, and stop at the third house on the left.”
The declarative version might sound something like this: “Drive to Sue’s house.” One says how
to do something; the other says what needs to be done.
A procedural Programming language follows a sequence of instructions and tells the computer
what to do step- by-step. Procedural programming depends on procedures. As procedural
programming language follows a method of solving problems from the top of the code to the
If the user wants to code a program, they would have to follow a sequence of instructions and
thereby enter the instructions. In addition, we can say that when a problem is need to be fix using
procedural programming, the developer will start with the problem (procedure) and then he
logically fragment the problem down into sub problems (Sub-Procedures). Subsequently, this
process will continue until a sub-procedure is simple enough to be solved by itself. Examples for
procedural programming languages include C, COBOL, FORTRAN and VB.
Object Oriented Programming or OOP is a programming paradigm that uses the concept of
classes and objects to construct models based on the real world surrounding. An object is a
constituent of a program that recognizes how to execute certain actions and how to interrelate
with other elements of the program. More emphasis is on data rather procedure. It can handle
almost all kind of real life problems which are today in scenario. Objects are the foundation of
object-oriented programming. An object-oriented program uses a set of objects, which will
communicate by sending and receiving messages to request services or information. A class is a
collection of objects with similar properties and behaviors (aka methods).
A simple example of an object would be a person. Logically, you would expect a person to have
a name. This would be considered a property of the person. You would also expect a person to
be able to do something, such as walking. This would be considered a method of the person. A
method in object-oriented programming is like a procedure in procedural programming. Finally,
an object or a collection of objects (class) attempts to complete its goals (goals such as
displaying ‘hello world’ on to the screen) by communicating by swapping messages. In fact,
displaying ‘Hello World’ is a method. Some examples for Object-Oriented Programming
languages include Java, C#.NET, C++, Python and Perl.
When we consider about the security of the data when using either of the programming
paradigms, OOP provides more security as it has a more improved data concealing mechanism
rather than procedural programming languages.
Procedural programming uses global data for sharing data within functions therefore data can be
accessed from function to function without any access limits. However, OOP does not allow
global data but instead the developer has the ability to set the functions to private or public so
developers can control the access rights for data.
In procedural programming, it is quite difficult to add new data or functions to the program but
OOP offers an easy approach to add new data and functions. Additionally, in procedural
programming data cannot be moved liberally from function to function but OOP allow objects to
move and communicate with each other via member functions.
Off course, you do not want to know the details, but on investigation you may find that Fred
delivered a slightly different message to another florist in the city where your friend Robin lives.
That florist then passes another message to a subordinate who makes the floral arrangement. The
flowers, along with yet another message, are passed onto a delivery person and so on. The
florists also have interactions with wholesalers who, in turn, had interactions with flower
growers and so on. This leads to our first conceptual picture of object-oriented programming:
Encapsulation
Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps
both safe from outside interference and misuse. One way to think about encapsulation is as a
protective wrapper that prevents the code and data from being arbitrarily accessed by other code
defined outside the wrapper. Access to the code and data inside the wrapper is tightly controlled
through a well-defined interface.
To relate this to the real world, consider the automatic transmission on an automobile. It
encapsulates hundreds of bits of information about your engine, such as how much you are
accelerating, the pitch of the surface you are on, and the position of the shift lever. You, as the
user, have only one method of affecting this complex encapsulation: by moving the gear-shift
lever. You can’t affect the transmission by using the turn signal or windshield wipers, for
example. Thus, the gear-shift lever is a well-defined (indeed, unique) interface to the
transmission. Further, what occurs inside the transmission does not affect objects outside the
The power of encapsulated code is that everyone knows how to access it and thus can use it
regardless of the implementation details—and without fear of unexpected side effects.
In Java/C++ the basis of encapsulation is the class. A class defines the structure and behavior
(data and code) that will be shared by a set of objects. Each object of a given class contains the
structure and behavior defined by the class, as if it were stamped out by a mold in the shape of
the class. For this reason, objects are sometimes referred to as instances of a class. Thus, a class
is a logical construct; an object has physical reality.
When you create a class, you will specify the code and data that constitute that class.
Collectively, these elements are called members of the class. Specifically, the data defined by the
class are referred to as member variables or instance variables. The code that operates on that
data is referred to as member methods or just methods. In properly written Java programs, the
methods define how the member variables can be used. This means that the behavior and
interface of a class are defined by the methods that operate on its instance data.
Inheritance
Inheritance is the process by which one object acquires the properties of another object. This is
important because it supports the concept of hierarchical classification. For example, a Golden
Retriever is part of the classification dog, which in turn is part of the mammal class, which is
under the larger class animal. Without the use of hierarchies, each object would need to define
all of its characteristics explicitly.
However, by use of inheritance, an object need only define those qualities that make it unique
within its class. It can inherit its general attributes from its parent. Thus, it is the inheritance
mechanism that makes it possible for one object to be a specific instance of a more general case.
Let’s take a closer look at this process.
If you wanted to describe a more specific class of animals, such as mammals, they would have
more specific attributes, such as type of teeth, and mammary glands. This is known as a subclass
of animals, where animals are referred to as mammals’ superclass. Since mammals are simply
more precisely specified animals, they inherit all of the attributes from animals. A deeply
inherited subclass inherits all of the attributes from each of its ancestors in the class hierarchy.
Fig: inheritance
Polymorphism
Polymorphism (Greek, meaning “many forms”) is a feature that allows one interface to be used
for a general class of actions. The specific action is determined by the exact nature of the
situation. Consider a stack (which is a last-in, first-out list). You might have a program that
requires three types of stacks. One stack is used for integer values, one for floating-point values,
and one for characters. The algorithm that implements each stack is the same, even though the
data being stored differs. In a non–object-oriented language, you would be required to create
three different sets of stack routines, with each set using different names. However, because of
polymorphism, in Java you can specify a general set of stack routines that all share the same
names.
More generally, the concept of polymorphism is often expressed by the phrase “one interface,
multiple methods.” This means that it is possible to design a generic interface to a group of
related activities. This helps reduce complexity by allowing the same interface to be used to
specify a general class of action. It is the compiler’s job to select the specific action (that is,
method) as it applies to each situation. You, the programmer, do not need to make this selection
manually. You need only remember and utilize the general interface.
There are several fundamentally different kinds of polymorphism:
If a function denotes different and potentially heterogeneous implementations depending
on a limited range of individually specified types and combinations, it is called ad
hoc polymorphism. Ad hoc polymorphism is supported in many languages using function
overloading.
If the code is written without mention of any specific data type and thus can be used
transparently with any number of new types, it is called parametric polymorphism. In the
object-oriented programming community, this is often known as generics or generic
void main() {
letsHear(new Cat());
letsHear(new Dog());
}
}
Polymorphism, Encapsulation, and Inheritance Work Together
When properly applied, polymorphism, encapsulation, and inheritance combine to produce a
programming environment that supports the development of far more robust and scalable
programs than does the process-oriented model. A well-designed hierarchy of classes is the basis
for reusing the code in which you have invested time and effort developing and testing.
Encapsulation allows you to migrate your implementations over time without breaking the code
that depends on the public interface of your classes. Polymorphism allows you to create clean,
sensible, readable, and resilient code.
To summarize:
In procedural languages, everything is a procedure.
In functional languages, everything is a function.
In logic programming languages, everything is a logical expression (predicate).
In object-oriented languages, everything is an object.