Object Oriented Programming with JAVA BCS306A
MODULE-I
Chapter 1 An Overview of Java
1.1 Object-Oriented Programming
➢ Object-oriented programming (OOP) is at the core of Java. In fact, all Java programs are to at
least some extent object-oriented. OOP is so integral to Java that it is best to understand its basic
principles before you begin writing even simple Java programs.
1.1.1 Two Paradigms
➢ All computer programs consist of two elements: code and data.
➢ Furthermore, a program can be conceptually organized around its code or around its data. That
is, some programs are written around “what is happening” and others are written around “who
is being affected.”
➢ These are the two paradigms that govern how a program is constructed.
➢ The first way is called the process-oriented model. This approach characterizes a program as a
series of linear steps (that is, code). The process-oriented model can be thought of as code
acting on data. Procedural languages such as C employ this model to considerable success.
However, problems with this approach appear as programs grow larger and more complex.
➢ To manage increasing complexity, the second approach, called object-oriented programming,
was conceived. Object-oriented programming organizes a program around its data (that is,
objects) and a set of well-defined interfaces to that data. An object-oriented program can be
characterized as data controlling access to code.
1.1.2 Abstraction
➢ Data abstraction is a virtue by which an object hides its internal operations from the rest of
the program. It makes it unnecessary for the client programs to know how the data is internally
arranged in the object.
➢ Humans manage complexity through abstraction. For example, people do not think of a car as a
set of tens of thousands of individual parts. They think of it as a well-defined object with its own
unique behavior.
➢ This abstraction allows people to use a car to drive to the grocery store without being
overwhelmed by the complexity of the parts that form the car. They can ignore the details of how
the engine, transmission, and braking systems work. Instead, they are free to utilize the object as
a whole.
➢ A powerful way to manage abstraction is through the use of hierarchical classifications. This
allows you to layer the semantics of complex systems, breaking them into more manageable
pieces. From the outside, the car is a single object.
1.1.3 The Three OOP Principles
➢ All object-oriented programming languages provide mechanisms that help you implement the
object-oriented model. They are encapsulation, inheritance, and polymorphism.
1. Encapsulation
➢ Encapsulation is the mechanism that binds together code and the data it manipulates, and
keeps both safe from outside interference and misuse.
1
, Object Oriented Programming with JAVA BCS306A
➢ 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.
➢ Further, what occurs inside the transmission does not affect objects outside the transmission. For
example, shifting gears does not turn on the headlights.
➢ In Java, 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.
➢ Since the purpose of a class is to encapsulate complexity, there are mechanisms for hiding the
complexity of the implementation inside the class. Each method or variable in a class may be
marked private or public.
➢ The public interface of a class represents everything that external users of the class need to
know, or may know. The private methods and data can only be accessed by code that is a
member of the class. Therefore, any other code that is not a member of the class cannot access a
private method or variable. Of course, this means that the public interface should be carefully
designed not to expose too much of the inner workings of a class (see Figure 2-1).
2
MODULE-I
Chapter 1 An Overview of Java
1.1 Object-Oriented Programming
➢ Object-oriented programming (OOP) is at the core of Java. In fact, all Java programs are to at
least some extent object-oriented. OOP is so integral to Java that it is best to understand its basic
principles before you begin writing even simple Java programs.
1.1.1 Two Paradigms
➢ All computer programs consist of two elements: code and data.
➢ Furthermore, a program can be conceptually organized around its code or around its data. That
is, some programs are written around “what is happening” and others are written around “who
is being affected.”
➢ These are the two paradigms that govern how a program is constructed.
➢ The first way is called the process-oriented model. This approach characterizes a program as a
series of linear steps (that is, code). The process-oriented model can be thought of as code
acting on data. Procedural languages such as C employ this model to considerable success.
However, problems with this approach appear as programs grow larger and more complex.
➢ To manage increasing complexity, the second approach, called object-oriented programming,
was conceived. Object-oriented programming organizes a program around its data (that is,
objects) and a set of well-defined interfaces to that data. An object-oriented program can be
characterized as data controlling access to code.
1.1.2 Abstraction
➢ Data abstraction is a virtue by which an object hides its internal operations from the rest of
the program. It makes it unnecessary for the client programs to know how the data is internally
arranged in the object.
➢ Humans manage complexity through abstraction. For example, people do not think of a car as a
set of tens of thousands of individual parts. They think of it as a well-defined object with its own
unique behavior.
➢ This abstraction allows people to use a car to drive to the grocery store without being
overwhelmed by the complexity of the parts that form the car. They can ignore the details of how
the engine, transmission, and braking systems work. Instead, they are free to utilize the object as
a whole.
➢ A powerful way to manage abstraction is through the use of hierarchical classifications. This
allows you to layer the semantics of complex systems, breaking them into more manageable
pieces. From the outside, the car is a single object.
1.1.3 The Three OOP Principles
➢ All object-oriented programming languages provide mechanisms that help you implement the
object-oriented model. They are encapsulation, inheritance, and polymorphism.
1. Encapsulation
➢ Encapsulation is the mechanism that binds together code and the data it manipulates, and
keeps both safe from outside interference and misuse.
1
, Object Oriented Programming with JAVA BCS306A
➢ 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.
➢ Further, what occurs inside the transmission does not affect objects outside the transmission. For
example, shifting gears does not turn on the headlights.
➢ In Java, 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.
➢ Since the purpose of a class is to encapsulate complexity, there are mechanisms for hiding the
complexity of the implementation inside the class. Each method or variable in a class may be
marked private or public.
➢ The public interface of a class represents everything that external users of the class need to
know, or may know. The private methods and data can only be accessed by code that is a
member of the class. Therefore, any other code that is not a member of the class cannot access a
private method or variable. Of course, this means that the public interface should be carefully
designed not to expose too much of the inner workings of a class (see Figure 2-1).
2