You are on page 1of 40

Java Programming

Course Code: IT 201


Concepts of OOPS
OOPS
(Object Oriented Programming
Structure)
OOPS stands for Object Oriented Programming Structure.
Object Oriented Programming develops a program around
its data and sets well-defined interface to that data.

An object-oriented program can be characterized as data controlling


access to code.

3/5/2018 3
CONCEPTS OF OOPS
• Objects
• Classes
• Encapsulation
• Data Abstraction
• Inheritance
• Polymorphism

3/5/2018 4
Objects

Objects is the run time entities in an object


– oriented system. An objects may
represent the city, name,person etc.

Object=Data+Method

3/5/2018 5
Classes
A class describes a group of objects with
similar attributes, operations and
relationship to other objects

Class object object object

Employee Emp name Emp age Emp salary

3/5/2018 6
Encapsulation
Encapsulation is the mechanism that binds
together code and the data it manipulates ,
and keeps both safe from outside
interference and misuse.

3/5/2018 7
Data Abstraction
• The act of representing essential features
without including the background details
or explanations.
• Classes use the concept of abstraction and
defined as a list of attributes, and
functions to operate these attributes.
• It’s also known as Abstract DataTypes(ADT)

8
Inheritance
Inheritance is the process by which objects of one
class acquires the properties of objects of another class.

The concept of inheritance provides the idea of reusability.


Animal

Mammal Reptile

Dog
Cat

9
Polymorphism
Polymorphism is a feature that allows one
interface to be used for a general class of actions
or
“one interface, multiple methods”

Shape
Draw ()

Box object Triangle object


Circle object
Draw(box) Draw(triangle)
Draw(circle)
10
Benefits of OOP
• Software reusability

• Code sharing

• Rapid prototyping

• Information hiding

11
Need For Java

• Java was conceived by James Gosling, Patrick


Naughton, Chris Warth,Ed Frank and Mike
Sheridan at Sun Microsystems,Inc. in 1991
• This language was initially called “Oak” by
James Gosling, one of the inventors of the
Java Language but was renamed “Java” in
1995.
• This original motivation for development of Java was not
the Internet! Primary motivation was the need for
platform-independent(that is, architecture neutral)
language that could be used to create software to be
embedded in various consumer electronics devices, such
as microwave ovens and remote controls. Java was
initially designed to solve on a small scale could also be
applied to the internet on a large scale .This realization
caused the focus of Java to switch from consumer
electronics to Internet Programming
JDK Versions
• JDK 1.02 (1995)
• JDK 1.1 (1996)
• Java 2 SDK v 1.2 ( JDK 1.2, 1998)
• Java 2 SDK v 1.3 (JDK 1.3, 2000)
• Java 2 SDK v 1.4 (JDK 1.4, 2002)

14
Features of Java
• Java is simple
• Java is object-oriented
• Java is distributed
• Java is interpreted and High Performance
• Java is robust
• Java is secure
• Java is architecture-neutral
• Java is portable
• Java’s performance
• Java is multithreaded
• Java is dynamic

15
Sun describes it as
"A simple, object-oriented,
distributed, interpreted, robust,
secure, architecture neutral,
portable, high-performance,
multi-threaded and dynamic
language."

16
Simple
• Java was designed to be easy for the professional
programmer to learn and use effectively

• Compared to many modern languages, there is not too much


to the core of Java. However, a great deal of functionality
exists in the standard libraries, and you will eventually need
to learn those libraries

Java omits many rarely used, poorly understood, confusing


features of C++. Say : No Pointer! No dynamic delete
Object Oriented
• The object model in java is simple and easy to extend

Object –oriented design is a technology that focuses design on


the data (object) and on the interfaces to it.

Everything is an object, everything will become a class in


Java. Every java program, in top- level view, is classes. Java
focusses on the creation of classes and objects. Every Java
program begins with a central class or object
Compiled and Interpreted
Usually a computer language is either compiled or interpreted
• Java combines both(compilation and interpretation) these
approaches thus making java a two-sage system
• First, Java compiler translates source code into what is
known as bytecode instructions
Bytecode are not machine instructions and therefore ,
• Second stage Java interpreter generates machine code that
can be directly executed by the machine that is running the
java program

We can say that java is both a compiled and interpreted language


Java is Compiled and Interpreted
Hardware and
Programmer
Operating System

Source Code Byte Code

Text Editor Compiler Interpreter


.java file .class file

Notepad, javac java


emacs,vi appletviewer
netscape
Interpreted Languages

Programmer

Source Code Object Executable


Code Code
Text Editor Compiler linker
.c file .o file a.out file

Notepad,
emacs,vi
Java is architecture-Neutral

A Java program will run identically (except for


speed) on every platform. Unlike most
languages, which can have a different
implementation on every platform
Architecture Neutral & Portable

• Java Compiler - Java source code (file with


extension .java) to bytecode (file with
extension .class)

• Bytecode - an intermediate form, closer to


machine representation

• A interpreter (virtual machine) on any


target platform interprets the bytecode.
Architecture Neutral & Portable

• Porting the java system to any new


platform involves writing an interpreter.

• The interpreter will figure out what the


equivalent machine dependent code to run
Portable
• Translating a Java program into byte code helps makes it
much easier to run a program in a wide variety of
environments
• JVM needs to be implemented for each platform. Once the
run-time package exits for a given system, any java program
can run on it.
• Details of the JVM will differ from platform to platform, even
though all interpret the same java bytecode.
• If a java program was compiled to native code, than different
versions of the same program would have to exist for each
type of CPU connected to the internet. This is ,of course ,not
a feasible solution. Thus ,the interpretation of bytecode is
the easiest way to create truly portable programs
Three features make Java String
programs portable
1 The language. The Java language is completely specified; all
data-type sizes and formats are defined as part of the language. By
contrast, C/C++ leaves these "details" up to the compiler
implementer, and many C/C++ programs are not portable

2 The library. The Java class library is available on any machine


with a Java runtime system, because a portable program is of no
use if you cannot use the same class library on every platform.
Window-manager function calls in a Mac application written in
C/C++, for example, do not port well to a PC

26
The byte code. The Java runtime system does not compile
your source code directly into machine language, an
inflexible and nonportable representation of your program.
Instead, Java programs are translated into machine-
independent byte code. The byte code is easily interpreted
and therefore can be executed on any platform having a
Java runtime system. (The latest versions of the Netscape
Navigator browser, for example, can run applets on
virtually any platform).

27
Robust
Java is robust language .It provides many safeguards to ensure reliable code. It
has strict compile time and runtime checking for data types.
It has several features designed to avoid crashes during program execution,
including:

• No pointer arithmetic
• Garbage collection--no bad addresses
• It incorporates the concept of exception handling which captures errors and
eliminate the risk of crashing the system
• No jumping to bad method addresses
• Interfaces and exceptions

The single biggest difference between Java and C/C++ is that Java has
“a inner safe pointer-model”, therefore it eliminates the possibility of
overwriting memory and corrupting data, so programmers feel very safe in
coding.

28
Security
The Java language is secure it is very difficult to write incorrect code or viruses
that can corrupt/steal your data, or harm hardware such as hard disks.Java system
not only verify all memory access but also ensure that no viruses are
communicated with an applet.

There are main lines of defense:


• Interpreter level:
• Absence of pointer in java ensures that programs cannot gain access to memory
locations without proper authorization
• Garbage collection
• No illegal data conversions
• Browser level (applies to applets only):
• No local file I/O

29
Distributed
• Java is designed for the distributed environment of the internet,
because it handles TCP/IP protocols.
• It has the ability share both data and programs.
• Java applications can open and access remote objects on internet
as easily as they can do in a local system.

• Java includes features for intra-address-space messaging. This


allows objects on two different computers to execute procedures
remotely. Basically, Java is for Net-Work application, for WEB
project.
• Oriented towards client/server systems rather than true
distributed systems

with the same ease as when accessing a local file system


Multithreaded
Multithreaded means handling multiple tasks simultaneously.
Java supports multiple programs.
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 multi-process synchronization that enables you to
construct smoothly running interactive systems This means
that we need not wait for the application to finish one task
before beginning another.

For example we can listen to an audio clip while scrolling a


page and at the same time download an applet from a distant
computer.
Dynamic
• Java is a dynamic language. Java is capable of dynamically linking
in new libraries ,methods and objects. Java can also determine
the type of class through a query, making it possible to either
dynamically link or abort the program
• New objects can easily be created, and new classes can be loaded
as the program runs
• 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.
High Performance

Java performance is impressive for an


interpreted language, mainly due to the use of
intermediate bytecode.
Incorporation of multithreading enhances the
overall execution speed of java programs
Reference Book

JavaTM 2:The complete Reference ,Fifth edition


How Does Java Compares to C++
and Other OO Languages
Overlap of C, C++, and Java

C++

C Java
JAVA and C
Java is lot like C but difference is java is object oriented language

• Java does not include C keywords like sizeof and typedef


• No data types like struct and union in C
• Modifiers like auto ,extern,register,signed and unsigned are
not defined by Java
• Java does not support explicit pointer type
• Java does not have preprocessor therefore we cannot use
#include,#define
• Java adds new operator such as instanceof and >>>
• Java adds labelled break and continue statements
• Java adds many features required for OOP
Java better than C++ ?
• No Preprocessors
• No Global Variables
• No Goto statements
• No Pointers
• No Unsafe Structures
• No Multiple Inheritance
• No Operator Overloading
• Java has replace destructor function with finalize()
function
• Does not have template classes as in c++
Object Oriented Languages -A Comparison

Feature C++ Objective Ada Java


C
Encapsulation Yes Yes Yes Yes
Inheritance Yes Yes No Yes
Multiple Inherit. Yes Yes No No
Polymorphism Yes Yes Yes Yes
Binding (Early or Late) Both Both Early Late
Concurrency Poor Poor Difficult Yes
Garbage Collection No Yes No Yes
Genericity Yes No Yes Limited
Class Libraries Yes Yes Limited Yes
Review Questions
Q How is Java more secured than other languages
Q Why is java known as platform neutral language
Q What is multithreading?