Java
CHAPTER 2
Programming paradigms, 3oop principles(PIE), Lexical issues,
Two Paradigms
• Procedure Oriented Programming (POP)
• Object Oriented Programming (OOP)
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 procedure oriented 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
Procedure Oriented Programming (POP) vs. Object
Oriented Programming (OOP)
In Procedure Oriented Programming,
• Emphasis is on doing things (algorithms)
• Large programs are divided into smaller programs known as functions
• Most of the function global data
• Data move openly around the system from function to function
• Functions transform data from one form to another
In OOP,
• Emphasis is on data rather than procedure
• Programs are divided into what we are known as objects
• Data structures are designed such that they characterizes the objects
• Functions that operate on the data of an object are tied together in
the data structure
• Data is hidden and cannot be accessed by external functions
• Object may communicate each other through functions
Abstraction
• Abstraction is the essence of OOP.
• Abstraction means the representation of the essential features
without providing the internal details and complexities.
• In OOP, abstraction is achieved by the help of class, where data and
methods are combined to extract the essential features only.
• For example, if we represent chair as an object it is sufficient for us to
understand that the purpose of chair is to sit.
• So we can hide the details of construction of chair and things required
to build the chair. It focuses the outside view of an object, separating
its essential behavior from its implementation.
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. (PIE)
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.
• 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.
• 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. (If you are familiar with C/C++, it may help
to know that what a Java programmer calls a
• 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.
Since the purpose of a class is to encapsulate complexity, there are
mechanisms for hiding the complexity of the implementation inside the
class
• Encapsulation is the process of combining the data (called fields or
attributes) and functions (called methods or behaviors) into a single
framework called class.
• Encapsulation helps preventing the modification of data from outside the
class by properly assigning the access privilege to the data inside the class.
• So the term data hiding is possible due to the concept of encapsulation,
since the data are hidden from the outside world so that it is safe from
accidental alteration.
Inheritance
• Inheritance is the process by which objects of one class acquire the
characteristics of object of another class.
• In OOP, the concept of inheritance provides the idea of reusability.
• We can use additional features to an existing class without modifying
it.
• This is possible by deriving a new class (derived class) from the
existing one (base class).
• This process of deriving a new class from the existing base class is
called inheritance .
• It supports the concept of hierarchical classification.
• It allows the extension and reuse of existing code without having to
rewrite the code.
Polymorphism
• Polymorphism means “having many forms”.
• The polymorphism allows different objects to respond to the same
message in different ways, the response specific to the type of object.
• It allows one interface to be used for a general class of actions.
• The specific action is determined by the exact nature of the situation.
• Polymorphism is important when object oriented programs
dynamically creating and destroying the objects in runtime.
• More generally, the concept of polymorphism is often expressed by
the phrase “one interface, multiple methods.”
• Polymorphism in java is a concept by which we can
perform a single action by different ways.
• Polymorphism is derived from two Greek words: poly and
morphs. The word "poly" means many and "morphs"
means forms.
• So polymorphism means many forms.
• There are two types of polymorphism in java:
compile time polymorphism and runtime polymorphism.
We can perform polymorphism in java by method
overloading and method overriding.
Lexical Issues
• Java programs are a collection of
• whitespace
• identifiers
• literals
• comments
• operators
• separators and
• keywords.
These are the atomic elements of a java program.
Whitespace
• Java is a free-form language. This means that you do not need to
follow any special indentation rules
• In Java, whitespace is a space, tab, or newline
Identifiers
Identifiers are used to name things, such as classes, variables, and
methods.
Literals
A constant value which can be assigned to a variable is called a literal
A constant value in Java is created by using a literal representation of it.
Comments
There are three types of comments defined by Java.
• Single line comment. // it is single line comment
• Multi line comment /* it is a comment this is in next line */
• Documentation comment:
This type of comment is used to produce an HTML file that documents
your program.
The documentation comment begins with a /** and ends with a */.
Separators
• In Java, there are a few characters that are used as separators.
• The most commonly used separator in Java is the semicolon which is used to
terminate statements.
Keyword:
Keywords are pre-defined or reserved words in a programming language.
Each keyword is meant to perform a specific function in a program. Since keywords
are referred names for a compiler, they can’t be used as variable names because by
doing so, we are trying to assign a new meaning to the keyword which is not
allowed.
Java language supports following keywords:
abstract assert boolean break byte case catch
char class const continue default do
double else enum exports extends final
finally float for goto if
implements import instanceof int interface long
module native new open opens package
private protected provides public requires return
short static strictfp super switch synchronized
this throw throws to transient transitive
try uses void volatile while with
• The keywords const and goto are reserved but not used.
• In addition to the keywords, Java reserves the following: true, false,
and null.
• These are values defined by Java. You may not use these words for the
names of variables, classes, and so on.