Object Oriented Programming in
Object Oriented Programming in
Object Oriented Programming in
Programming in
1
Introduction to Object-
Oriented Programming
(OOP)
Types of Programming
Paradigm
2
Overview of Computer Programming
3
Cont…
4
Programming Paradigm
5
Classification of programming paradigms
• Imperative paradigms
– It is based on commands that update variables in
storage.
– commands show how the computation takes place, step
by step. Each step affects the global state of the
computation.
– The Latin word imperare means “to command”.
– The language provides statements, such as assignment
statements, which explicitly change the state of the
memory of the computer.
– Algol, Cobol, PL/1, Ada, C, Modula-3
6
Functional programming paradigms
• In this paradigm we express computations as
the evaluation of mathematical functions.
• Control flow is expressed by combining
function calls, rather than by assigning values
to variables.
• treat values as single entities.
• Lisp, Haskell, ML, Miranda, APL.
7
Logic programming paradigms
• In this paradigm we express computation in
exclusively in terms of mathematical logic.
• Focuses on predicate logic, in which the basic
concept is a relation.
• Programs are built by setting up relations that
specify facts and inference rules, and asking
whether or not something is true (i.e. specifying a
goal.)
• Prolog, GHC, Parlog, Vulcan, Polka, Mercury, Fnil.
8
The Object-Oriented Paradigm
• a new features and new way of thinking about the
process of decomposing problems and developing
programming solutions.
• Everything is modeled as object
• Computation is performed by message passing: objects
communicate with one another via message passing.
• Every object is an instance of a class where a class
represents a grouping of similar objects.
• Inheritance: defines the relationships between classes.
• focuses on the objects that a program is representing,
and on allowing them to exhibit "behavior".
• Smalltalk , Simula , C++, C#, Ruby, Java 9
1.2 Overview of OO principles
Introduction:
• An object is a software bundle of related state
and behavior.
• Real-world objects share two characteristics:
They all have state and behavior.
• Eg. Dogs have state (name, color, breed) and
behavior (barking, wagging tail).
• Identifying the state and behavior for real-
world objects is a great way to begin thinking
in terms of OOP.
10
Cont…
• An object stores its state in fields (variables in
some programming languages) and exposes its
behavior through methods (functions in some
programming languages).
• Methods operate on an object's internal state and
serve as the primary mechanism for object-to-
object communication.
• A class is the blueprint from which individual
objects are created.
11
Four Principles of Object
Oriented Programming:
– Data Abstraction
– Encapsulation
– Inheritance
– Polymorphism
12
Data abstraction
• Any representation of data in which the
implementation details are hidden (abstracted).
• Abstract data types and objects are the two
primary forms of data abstraction.
• Data abstraction is the development of classes,
objects, types in terms of their interfaces and
functionality, instead of their implementation
details.
• It is the development of a software object to
represent an object we can find in the real world.
• Abstraction is used to manage complexity.
13
Encapsulation
• Encapsulation is the hiding of data
implementation by restricting access.
• i.e hiding data and methods within an Object.
• Encapsulation provides the security that keeps
data and methods safe from inadvertent
changes.
14
Inheritance
• An important feature of OOP is inheritance.
• Inheritance the ability to create classes that
share the attributes and methods of existing
classes, but with more specific features.
• Inheritance is mainly used for code reusability.
15
Polymorphism
• Polymorphism definition is that Poly means
many and morphos means forms.
• Polymorphism means one name, many forms.
• It describes the feature of languages that allows
the same word or symbol to be interpreted
correctly in different situations based on the
context.
16
Java
Programming Language
OOP Perspective
17
Overview of Java
• Java is a computer programming language that is
concurrent, class-based, object-oriented, and specifically
designed to have as few implementation dependencies as
possible.
• Java is guaranteed to be Write Once, Run Anywhere
(WORA)
• Java runs on a variety of platforms, such as Windows,
Mac OS, and the various versions of UNIX.
• Java is now used to develop large-scale enterprise
applications, to enhance the functionality of web servers,
to provide applications for consumer devices (e.g., cell
phones, pagers and PDAs) and for many other purposes.
18
Cont’d
• Java technology can be seen as both a language and a
platform.
• Using it you can write applications that can run on
practically any device, including a PC, PDA, a cellular
phone, a television and other.
• The Java platform is formed from two components:
– The Java Application Programming Interface (Java API)
• Set if libraries that are used to accomplish tasks such
as creating GUIs, performing file I/O and establishing
network communication.
– The Java Virtual Machine (JVM)
• Is in charge of executing your code in a specific
environment. 19
Types of Java Applications
• There are mainly 4 type of applications that can be
created using java programming:
1) Standalone Application
It is also known as desktop application or window-
based application. An application that we need to
install on every machine such as media player, antivirus
etc. AWT and Swing are used in java for creating
standalone applications.
2) Web Application
An application that runs on the server side and creates
dynamic page, is called web application. Currently,
Servlet, JSP, Struts, JSF etc. technologies are used for
creating web applications in java.
Cont’d
3) Enterprise Application
An application that is distributed in nature, such
as banking applications etc.
It has the advantage of high level security, load
balancing and clustering.
In java, EJB is used for creating enterprise
applications.
4) Mobile Application
An application that is created for mobile devices.
Currently Android and Java ME are used for
creating mobile applications.
The History of Java
• Comes out of ‘Green Project’ in 1991.
• Created by ‘James Gosling’ and called it Oak.
• Released to the public in 1995 by Sun Microsystems.
• It was completely processor-independent.
• Its primary feature is that it could function nicely in a
networked environment.
• The explosion of WWW created an environment in
which the language could live.
• On 13 November 2006, Sun released much of Java
as free and open source software under the terms of
the GNU General Public License (GPL). 22
The Features of Java Technology
There is given many features of java.
They are also known as Java buzzwords.
The Java Features given below are simple and easy
to understand.
1. Object-oriented 6. Robust
2. Simple 7. Distributed
3. Platform independent 8. Secured
4. Interpreted 9. Multithreaded
5. Portable 10.High performance
23
Cont’d
• Java is Object Oriented
– In Java, everything is an Object.
– Java can be easily extended since it is based on the Object model.
– The OOP paradigm has risen in popularity and has become the de
factor standard for today’s software dev’t.
• Java is Simple
– Java is designed to be easy to learn. If you understand the basic
concept of OOP, Java would be easy to master.
• Platform independent
– Java is compiled into platform independent byte code.
• Java is Interpreted
– Java source code is passed to a compiler that generates the byte
code.
– JVM interprets the byte code at runtime and executes it.
24
Cont’d
• Java is portable
– Java applications can run practically anywhere.
– You can truly write the code once and run it anywhere.
• Java is Robust
– Robust code is reliable code.
– Java is considered a strongly typed language.
– JVM insures robustness managing all memory automatically.
– Java also includes an extensible mechanism for exception
handling.
• Java is Distributed
– Java is designed for the distributed environment of the
Internet.
25
Cont’d
• Java is Secured
– Because Java technology is so prevalent on networks and in
today’s enterprise systems, it security aspects as vital.
– With Java's secure feature, it enables to develop virus-free, tamper-
free systems.
• Java is Multithreaded
– With Java's multithreaded feature, it is possible to write programs
that can do many tasks simultaneously.
– This design feature allows developers to construct smoothly
running interactive applications.
• Java is High Performance
– Java versions have performed at the speed that developers demand.
– With the use of Just-In-Time (JIT) compilers, Java enables high
performance.
26
Types of Java Program
• Different ways to write/run a Java codes are:
Application:
A stand-alone program that can be invoked from command
line .
A program that has a “main” method
Applet:
A program embedded in a web page , to be run when the page
is browsed.
A program that contains no “main” method
• Application -Java interpreter
• Applets- Java enabled web browser (Linked to
HTML via <APPLET> tag. in html file)
27
Typical Java Development Environment
• Java programs normally go through five phases-
edit, compile, load, verify and execute.
• We discuss these phases in the context of the JDK 1.7
from Sun Microsystems, Inc.,
• Java Development Kit (JDK) contains tools needed to
develop the Java programs, and JRE to run the programs.
• The tools include compiler (javac.exe), Java application
launcher (java.exe), Appletviewer, etc…
• Compiler converts java code into byte code. Java
application launcher opens a JRE (java Runtime
Environment), loads the class, and invokes its main
method.
28
Java Development Environment
29
Phase 1: Creating a Program
• Phase 1 consists of editing a file with an
editor program ( eg. Notepad).
• You type a Java program (source code) using
the editor, make any necessary corrections and
save the program on a secondary storage
device, such as your hard drive.
• A file name ending with the .java extension
indicates that the file contains Java source
code.
30
Cont’d
• For organizations that develop substantial
information systems, Integrated Development
Environments (IDEs) are available from many
major software suppliers, including Sun
Microsystems.
• IDEs provide tools that support the software
development process, including editors for writing
and editing programs and debuggers for locating
logic errors.
• E.g NetBeans, Eclipse, jEdit, etc.
31
Phase 2: Compiling a Java Program into
Bytecodes
• In Phase 2, the programmer uses the command
javac (the Java compiler) to compile a program.
• For example, to compile a program called
Welcome.java, you would type
javac Welcome1.java
in the command window of your system.
• If the program compiles, the compiler produces
a .class file called Welcome.class that contains the
compiled version of the program.
32
Phase 2: Compiling a Java Program into Byte codes
• The Java compiler translates Java source code into byte codes.
• Byte codes are executed by the Java Virtual Machine (JVM)
• JVM a part of the JDK and the foundation of the Java platform.
• Byte codes are platform-independent instructions.
• So Java’s bytecodes are portable—that is, the same bytecodes
can execute on any platform containing a JVM that understands
the version of Java in which the bytecodes were compiled.
• The JVM is invoked by the java command.
• For example, to execute a Java application called Welcome, you
would type the command
Java Welcome1
in a command window to invoke the JVM, which would then
initiate the steps necessary to execute the application.
This begins Phase 3. 33
Phase 3: Loading a Program into Memory
35
Phase 5: Execution
• In Phase 5, the JVM executes the program’s byte codes,
thus performing the actions specified by the program.
• JVMs typically execute byte codes using a combination
of interpretation and so-called just-in-time (JIT)
compilation.
• In this process, The JVM analyzes the byte codes as
they are interpreted, searching for hot spots— parts of
the byte codes that execute frequently.
• For these parts, a just-in-time (JIT) compiler—
known as the Java HotSpot compiler—translates the
byte codes into the underlying computer’s machine
language.
36
Cont…
• When the JVM encounters these compiled parts
again, the faster machine-language code executes.
• Thus Java programs actually go through two
compilation phases:
1. source code is translated into byte codes (for
portability across JVMs on different computer
platforms) and
2. byte codes are translated into machine language
for the actual computer on which the program
executes.
37
HOW JAVA WORKS?
The Java compiler translates Java source code
into a special representation called byte code
Java byte code is not the machine language
for any traditional CPU.
Another software tool, called an interpreter
translates byte code into machine language
and executes it.
The JVM is a software layer that provides
translation between Java byte codes and the
native operating system.
38
How Java Works?
39
Editing, Compiling and
Interpreting Java program
40
Writing a Program
• The easiest way to write a simple Java program is
with a text editor (eg. Notepad, Netbeans,
Eclipse, jEdit,…).
• So, using the text editor of your choice, create a
text file with the following text, and be sure to
name the text file Hello.java
• Java programs are case sensitive, so if you type
the code in yourself, pay particular attention to the
capitalization.
41
Writing a Program
A First Program in Java: Printing a Line of Text
/*
This is a simple Java program.
Call this file “Hello.java".
*/
43
Interpreting and Running the Program
• Interpreting and running a Java program means
invoking the JVM byte code interpreter, which
converts the Java byte codes to platform-dependent
machine codes, so your computer can understand
and run the program.
• The Java interpreter is invoked at the command line
on Unix and DOS shell operating systems as
follows:
java Hello
• At the command line, you should see the following
output:
Welcome to Java World! 44