You are on page 1of 54

UNIT-1

INTRODUCTION TO OBJECT
ORIENTED PARADIGM

gourisankar.mishra@sharda.ac.in
+91-9654249954
Unit A Introduction to Object Oriented Paradigm

Unit A Introduction: History, The meaning of Object


Topic 1 Orientation, Features of Java, OOPs concepts

Unit A object identity, Encapsulation, information hiding,


Topic 2 polymorphism, inheritance

Unit A Java virtual machine, Byte Code, Architecture of JVM,


Topic 3 Class Loader, Execution Engine, Garbage collection,
HISTORY OF JAVA PROGRAMMING

 Green Team in Sun Micro Systems : June 1991


 James Gosling

 Mike Sheridan

 Patrick Naughton
HISTORY OF JAVA PROGRAMMING
CONTD…
 The first name was: Greentalk
 File extension: .gt

 A programming language was required for digital devices, home


appliances: televisions, set-top boxes etc.
 Requirement of a language which can execute in different
architectures
 The next name was Oak

 The national tree of USA

An Oak Tree in front of


Gosling’s Office
HISTORY OF JAVA PROGRAMMING
CONTD…

 Java Island of Indonesia


 Generic name of coffee is Java in many parts of world , like
North America
THE MEANING OF OBJECT ORIENTATION
THE MEANING OF … CONTD…
 Object means a real word entity such as pen, chair, table etc.

 Object-Oriented Programming is a methodology or paradigm to


design a program using classes and objects.

 It simplifies the software development and maintenance by providing


some concepts:
 Object
 Class
 Inheritance
 Polymorphism
 Abstraction
 Encapsulation
 Message Passing
OOPS CONCEPTS
OOP CONCEPTS CONTD…
 A class is a blueprint for any functional entity which defines its
properties and its functions.
 e.g. a car
ENCAPSULATION
 Encapsulation is the mechanism that binds together code and the
data it manipulates
 It also 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.
ENCAPSULATION CONTD…
CONTD…
 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.
ABSTRACTION
 An essential element of object-oriented programming is abstraction.
 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.
INHERITANCE
 Inheritance is the process by which one object acquires the
properties of another object.

 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.
EXAMPLE

 The properties of vehicle are defined in Vehicle class.


 Both Car & Truck inherit these properties; and they can add their
own specific properties.
POLYMORPHISM

 Polymorphism (from the 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.
 In java method overloading and method overriding is allowed.
 Methods with same name but different no of arguments or, order
of arguments distinguishes between called methods
MESSAGE PASSING

 Message passing by

the objects invoking

methods.
FEATURES OF JAVA
 The features of Java or, Java buzzwords:
 Simple
 Secure
 Portable
 Object-oriented
 Robust
 Multithreaded
 Architecture-neutral
 Interpreted
 High performance
 Distributed
 Dynamic
SIMPLE

 Java was designed to be easy for the professional programmer to


learn and use effectively.
 Java inherits the C/C++ syntax and many of the object-oriented
features of C++.
 Beyond its similarities with C/C++, Java has another attribute
that makes it easy to learn: it makes an effort not to have
surprising features.
 In Java, there are a small number of clearly defined ways to
accomplish a given task.
OBJECT-ORIENTED
 Although influenced by its predecessors, Java was not designed to be source-
code compatible with any other language.
 This allowed the Java team the freedom to design with a blank slate.
 One outcome of this was a clean, usable, pragmatic approach to objects.
 Borrowing liberally from many seminal object-software environments of the
last few decades, Java manages to strike a balance between the purist’s
“everything is an object” paradigm and the pragmatist’s “stay out of my way”
model.
 The object model in Java is simple and easy to extend, while simple types,
such as integers, are kept as high-performance nonobjects.
ROBUST

 The multiplatformed environment of the Web places extraordinary demands


on a program, because the program must execute reliably in a variety of
systems.
 Thus, the ability to create robust programs was given a high priority in the
design of Java.
 To gain reliability, Java restricts you in a few key areas, to force you to find
your mistakes early in program development. At the same time, Java frees you
from having to worry about many of the most common causes of
programming errors.
 It checks your code at compile time, it also checks your code at run time.
CONTD…
 To better understand how Java is robust, consider two of the main reasons for program
failure: memory management mistakes and mishandled exceptional conditions (that is,
run-time errors).
 Memory management can be a difficult, tedious, i.e. the programmer must manually
allocate and free all dynamic memory. This sometimes leads to problems, because
programmers will either forget to free memory that has been previously allocated or,
worse, try to free some memory that another part of their code is still using.
 Java virtually eliminates these problems by managing memory allocation and deallocation
for you. (In fact, deallocation is completely automatic, because Java provides garbage
collection for unused objects.)
 Exceptional conditions in traditional environments often arise in situations such as division
by zero or “file not found,” Java
 helps in this area by providing object-oriented exception handling. In a well-written Java
program, all run-time errors can—and should—be managed by your program.
MULTITHREADED

 Java was designed to meet the real-world requirement of creating


interactive, networked programs.
 To accomplish this, Java supports multithreaded programming, which
allows you to write programs that do many things simultaneously.
 The Java run-time system comes with an elegant yet sophisticated
solution for multiprocess synchronization that enables you to construct
smoothly running interactive systems.
 Java’s easy-to-use approach to multithreading allows you to think
about the specific behavior of your program, not the multitasking
subsystem.
ARCHITECTURE-NEUTRAL
 A central issue for the Java designers was that of code longevity and
portability. One of the main problems facing programmers is that no
guarantee exists that if you write a program today, it will run tomorrow
—even on the same machine.
 Operating system upgrades, processor upgrades, and changes in core
system resources can all combine to make a program malfunction.
 The Java designers made several hard decisions in the Java language
and the Java Virtual Machine in an attempt to alter this situation.
 Their goal was “write once; run anywhere, any time, forever.” To a
great extent, this goal was accomplished.
INTERPRETED AND HIGH PERFORMANCE

 Java enables the creation of cross-platform programs by compiling into an


intermediate representation called Java bytecode.
 This code can be interpreted on any system that provides a Java Virtual
Machine.
 The Java bytecode was carefully designed so that it would be easy to translate
directly into native machine code for very high performance by using a just-
in-time compiler.
 Java run-time systems that provide this feature lose none of the benefits of the
platform-independent code.
 “High-performance cross-platform” is achieved.
DISTRIBUTED
 Java is designed for the distributed environment of the Internet,
because it handles TCP/IP protocols. In fact, accessing a resource
using a URL is not much different from accessing a file.

 The original version of Java (Oak) included features for


intraaddress-space messaging. This allowed objects on two
different computers to execute procedures remotely.

 Java revived these interfaces in a package called Remote Method


Invocation (RMI). This feature brings an unparalleled level of
abstraction to client/ server programming.
DYNAMIC

 Java programs carry with them substantial amounts of run-time


type information that is used to verify and resolve accesses to
objects at run time.

 This makes it possible to dynamically link code in a safe and


expedient manner.

 This is crucial to the robustness of the applet environment, in


which small fragments of bytecode may be dynamically updated
on a running system.
SECURITY
 Prior to Java, most users did not download executable programs frequently,
and those who did scanned them for viruses prior to execution.
 In addition to viruses, another type of malicious program exists that must be
guarded against which searches the contents of your computer’s local file
system.
 Java answers both of these concerns by providing a “firewall” between a
networked application and your computer.

 When you use a Java-compatible Web browser, you can safely download Java
applets without fear of viral infection or malicious intent. Java achieves this
protection by confining a Java program to the Java execution environment
and not allowing it access to other parts of the computer.

 The ability to download applets with confidence that no harm will be done
and that no security will be breached is considered by many to be the single
most important aspect of Java.
PORTABILITY

 As discussed earlier, many types of computers and operating


systems are in use throughout the world—and many are
connected to the Internet.
 For programs to be dynamically downloaded to all the various
types of platforms connected to the Internet, some means of
generating portable executable code is needed.
 As you will soon see, the same mechanism that helps ensure
security also helps create portability.
 Indeed, Java’s solution to these two problems is both elegant and
efficient.
JAVA’S MAGIC: THE BYTECODE
 Bytecode is a highly optimized set of instructions designed to be executed by Java
Virtual Machine (JVM).
 The JVM is an interpreter for bytecode.
 Translating a Java program into bytecode helps makes it much easier to run a program in
a wide variety of environments.
 Although the details of the JVM will differ from platform to platform, all interpret the
same Java bytecode.
 The interpretation of bytecode is the easiest way to create truly portable programs.
 The execution of every Java program is under the control of the JVM, the JVM can
contain the program and prevent it from generating side effects outside of the system.
 The use of bytecode enables the Java run-time system to execute programs much faster
than you might expect.
OBTAINING THE JAVA ENVIRONMENT

 We can download the JDK (Java Development Kit) including the

compiler and runtime engine from Sun at:

https://www.oracle.com/technetwork/java/javase/downloads/index.

html

Install JDK after downloading, by default JDK will be installed in

C:\Program Files\Java\jdk1.5.0_05

(Here jdk1.5.0_05 is JDK‟s version)


SETTING UP JAVA ENVIRONMENT
 After installing the JDK, we need to set at least one environment
variable in order to able to compile and run Java programs.
 Go to the following:

Control Panel\System and Security\System


Opt- Advance setting, opt Advanced, click Environment variables
 A PATH environment variable enables the operating system to
find the JDK executables when our working directory is not the
JDK's binary directory.
Setting environment variables from a command prompt:
 If we set the variables from a command prompt, they will only
hold for that session.
 To set the PATH from a command prompt:

set PATH=C:\Program Files\Java\jdk1.5.0_05\bin;%PATH%


SETTING ENVIRON MENT VARIABLES AS SYSTEM
VARIABLES
 If we set the variables as system variables they will hold
continuously.
 Right-click on My Computer
 Choose Properties
 Select the Advanced tab
 Click the Environment Variables button at the bottom
 In system variables tab, select path (system variable) and
 click on edit button
 A window with variable name- path and its value will be
displayed.
 Don‟t disturb the default path value that is appearing and

just append (add) to that path at the end:


;C:\ProgramFiles\Java\jdk1.5.0_05\bin;
 Finally press OK button.
CLASSPATH SETTING
 To set the classpath for the Windows Command Prompt:
 Select Start -> Control Panel -> System -> Advanced ->
Environment Variables -> System Variables -> CLASSPATH.
 If the Classpath variable exists, prepend .;C:\introcs to the
beginning of the CLASSPATH varible.
 If the CLASSPATH variable does not exist, select New. Type
CLASSPATH for the variable name and .;C:\introcs for the
variable value.
 Click OK three times.
 Creating a Source File:
 Type the program in a text editor (i.e. Notepad, WordPad,
Microsoft Word or Edit Plus).
 We can launch the Notepad editor from the Start menu by
selecting Programs > Accessories >Notepad.
 Save the program with filename same as Class_name (i.e.
Sample.java) in which main method is written. To do this in
Notepad, first choose the File > Save menu item. Then, in the
Save dialog box
COMPILING THE SOURCE FILE INTO
A .CLASS FILE

 To Compile the Sample.java program go to DOS prompt: Start


menu by choosing Run... and then entering cmd.
 The prompt shows current directory. change current
 directory to the directory where Sample.java file is located.
 At the prompt, type the following command and press Enter.

javac Sample.java
 The compiler generates byte code and Sample.class will be
created.
EXECUTING THE PROGRAM (SAMPLE.CLASS)
 To run the program, enter java followed by the class name created
at the time of compilation at the command prompt in the same
directory as:

java Sample
 The program interpreted and the output is displayed.
The Java Virtual Machine:
 Java Virtual Machine (JVM) is the heart of entire Java program
execution process.
 First of all, the .java program is converted into a .class file
consisting of byte code instructions by the java compiler at the time
of compilation.
 Remember, this java compiler is outside the JVM. This .class file is
given to the JVM.
JAVA COMPILER & JVM ( JAVA VIRTUAL
MACHINE )
JVM MAKES JAVA PLATFORM INDEPENDENT
INTERNAL ARCHITECTURE OF JVM
CONTD…

 In JVM, there is a module (or program) called class loader sub


system, which performs the following instructions:
 First of all, it loads the .class file into memory.
 Then it verifies whether all byte code instructions are proper or
not. If it finds any instruction suspicious, the execution is rejected
immediately.
 If the byte instructions are proper, then it allocates necessary
memory to execute the program. This memory is divided into 5
parts, called run time data areas, which contain the data and
results while running the program. These areas are as follows:
CONTD…
 Method area: Method area is the memory block, which stores the class code,
code of the variables and code of the methods in the Java program.
 Heap: This is the area where objects are created. Whenever JVM loads a
class, method and heap areas are immediately created in it.
 Java Stacks: Method code is stored on Method area. But while running a
method, it needs some more memory to store the data and results. This
memory is allotted on Java Stacks. So, Java Stacks are memory area where
Java methods are executed. While executing methods, a separate frame will
be created in the Java Stack, where the method is executed. JVM uses a
separate thread (or process) to execute each method.
CONTD…

 PC (Program Counter) registers: These are the memory areas which


contain memory address of the instructions of the methods. If there are
3 methods, 3 PC registers will be used to track the instruction of the
methods.

 Native Method Stacks: Java methods are executed on Java Stacks.


Similarly, native methods (for example C/C++ functions) are executed
on Native method stacks. To execute the native methods, generally
native method libraries (for example C/C++ header files) are required.
These header files are located and connected to JVM by a program,
called Native method interface.
CONTD…

 Execution Engine contains interpreter and JIT compiler which


translates the byte code instructions into machine language which
are executed by the microprocessor.
 Hot spot (loops/iterations) is the area in .class file i.e. executed by
JIT compiler.
 JVM will identify the Hot spots in the .class files and it will give
it to JIT compiler where the normal instructions and statements of
Java program are executed by the Java interpreter.
JVM

 The JVM takes .class file as input


 Bytecode Verifier verifies the byte code

 The interpreter of JVM is JIT interpreter

 The memory management unit is also the part of JVM which is


used for running the codes
 JVM interacts with the operating system of the machine
THE JAVA RUNTIME ENVIRONMENT
 The Java application environment performs as follows:
JRE CONTD…
 The JVM performs three main tasks:
 Loads code
 Verifies code
 Executes code

 The Class Loader


 Loads all classes necessary for the execution of a program
 Maintains classes of the local file system in separate namespaces

 The Bytecode Verifier Ensures that:


 The code adheres to the JVM specification.
 The code does not violate system integrity.
 The code causes no operand stack overflows or underflows.
 The parameter types for all operational code are correct.
 No illegal data conversions (the conversion of integers to pointers)
have occurred.
GARBAGE COLLECTION
 Allocated memory that is no longer needed should be
deallocated.

 In other languages, deallocation is the programmer’s


responsibility.

 The Java programming language provides a system-level thread


to track memory allocation.

 Garbage collection has the following characteristics:


 Checks for and frees memory no longer needed
 Is done automatically
 Can vary dramatically across JVM implementations
JAVA DEVELOPMENT KIT (JDK)
COMPARISION

You might also like