You are on page 1of 14

Platform independent

Unlike many other programming languages


including C and C++ when Java is compiled, it is
not compiled into platform specific machine,
rather into platform independent byte code. This
byte code is distributed over the web and
interpreted by virtual Machine (JVM) on
whichever platform it is being run.
Object Oriented Programming

Since Java is an object oriented programming


language it has following features:
 Reusability of Code
 Emphasis on data rather than procedure
 Data is hidden and cannot be accessed by
external functions
 Objects can communicate with each other
through functions
 New data and functions can be easily added
Java has powerful features. The following are
some of them:-

Simple
Reusable
Portable (Platform Independent)
Distributed
Robust
Secure
High Performance
Dynamic
Threaded
Interpreted
Object Oriented Programming is a method of
implementation in which programs are organized
as cooperative collection of objects, each of which
represents an instance of a class, and whose
classes are all members of a hierarchy of classes
united via inheritance relationships.
OOP Concepts
Four principles of Object Oriented Programming
are
Abstraction
Encapsulation
Inheritance
Polymorphism
Abstraction
Abstraction denotes the essential characteristics of
an object that distinguish it from all other kinds of
objects and thus provide crisply defined
conceptual boundaries, relative to the perspective
of the viewer.
Encapsulation

Encapsulation is the process of


compartmentalizing the elements of an abstraction
that constitute its structure and behavior ;
encapsulation serves to separate the contractual
interface of an abstraction and its implementation.
Encapsulation
* Hides the implementation details of a class.
* Forces the user to use an interface to access data
* Makes the code more maintainable.
Inheritance
Inheritance is the process by which one object
acquires the properties of another object.
Polymorphism
Polymorphism is the existence of the classes or
methods in different forms or single name
denoting different
implementations.
Java is Distributed
With extensive set of routines to handle TCP/IP
protocols like HTTP and FTP java can open and
access the objects across net via URLs.
Java is Multithreaded
One of the powerful aspects of the Java language
is that it allows multiple threads of execution to
run concurrently within the same program A single
Java program can have many different threads
executing independently and continuously.
Multiple Java applets can run on the browser at the
same time sharing the CPU time.
Java is Secure
Java was designed to allow secure execution of
code across network. To make Java secure many
of the features of C and C++ were eliminated. Java
does not use Pointers. Java programs cannot access
arbitrary addresses in memory.
Garbage collection
Automatic garbage collection is another great
feature of Java with which it prevents inadvertent
corruption of memory. Similar to C++, Java has a
new operator to allocate memory on the heap for a
new object. But it does not use delete operator to
free the memory as it is done in C++ to free the
memory if the object is no longer needed. It is
done automatically with garbage collector.
Method Overloading
Method overloading results when two or more
methods in the same class have the same name but
different parameters. Methods with the same name
must differ in their types or number of parameters.
This allows the compiler to match parameters and
choose the correct method when a number of
choices exist. Changing just the return type is not
enough to overload a method, and will be a
compile-time error. They must have a different
signature. When no method matching the input
parameters is found, the compiler attempts to
convert the input parameters to types of greater
precision. A match may then be found without
error. At compile time, the right implementation is
chosen based on the signature of the method call
Overriding
Method Overriding is achieved when a
subclass overrides non-static methods defined
in the superclass, following which the new
method implementation in the subclass that is
executed.
The new method definition must have the
same method signature (i.e., method name and
parameters) and return type. Only parameter
types and return type are chosen as criteria for
matching method signature. So if a subclass
has its method parameters as final it doesn’t
really matter for method overriding scenarios
as it still holds true. The new method
definition cannot narrow the accessibility of
the method, but it can widen it. The new
method definition can only specify all or none,
or a subset of the exception classes (including
their subclasses) specified in the throws clause
of the overridden method in the super class

Classes
A class is nothing but a blueprint for creating
different objects which defines its properties and
behaviors. An object exhibits the properties and
behaviors defined by its class. A class can contain
fields and methods to describe the behavior of an
object. Methods are nothing but members of a
class that provide a service for an object or
perform some business logic.
Objects
An object is an instance of a class created using a
new operator. The new operator returns a reference
to a new instance of a class. This reference can be
assigned to a reference variable of the class. The
process of creating objects from a class is called
instantiation. An object reference provides a
handle to an object that is created and stored in
memory. In Java, objects can only be manipulated
via references, which can be stored in variables.
Interface
An Interface is a contract in the form of collection
of method and constant declarations. When a class
implements an interface, it promises to implement
all of the methods declared in that interface.

THREADS
A thread is an independent sequential path of
execution within a program. Many threads can run
concurrently within a program. At runtime, threads
in a program exist in a common memory space and
can, therefore, share both data and code, that is,
they are lightweight compared to processes. They
also share the process running the program.
Every thread in Java is created and controlled by a
unique object of the java.lang.Thread class.
Often the thread and its associated Thread object
are thought of as being synonymous

Multithreading has several advantages over


Multiprocessing such as;
 Threads are lightweight compared to processes
 Threads share the same address space and
therefore can share both data and code
 Context switching between threads is usually
less expensive than between processes
 Cost of thread intercommunication is
relatively low that that of process
intercommunication
 Threads allow different tasks to be performed
concurrently.
Thread Creation
There are two ways to create thread in java;
 Implement the Runnable interface
(java.lang.Runnable)
 By Extending the Thread class
(java.lang.Thread)
Implementing the Runnable Interface
The Runnable Interface Signature
public interface Runnable {
void run();
}
The procedure for creating threads based on the
Runnable interface is as follows:
1. A class implements the Runnable interface,
providing the run() method that will be executed
by the thread. An object of this class is a Runnable
object.
2. An object of Thread class is created by passing
a Runnable object as argument to the Thread
constructor. The Thread object now has a
Runnable object that implements the run() method.
3. The start() method is invoked on the Thread
object created in the previous step. The start()
method returns immediately after a thread has
been spawned.
4. The thread ends when the run() method ends,
either by normal completion or by throwing an
uncaught exception.
Extending Thread Class
The procedure for creating threads based on
extending the Thread is as follows:
1. A class extending the Thread class overrides the
run() method from the Thread class to define the
code executed by the thread.
2. This subclass may call a Thread constructor
explicitly in its constructors to initialize the thread,
using the super() call.
3. The start() method inherited from the Thread
class is invoked on the object of the class to make
the thread eligible for running.
When creating threads, there are two reasons why
implementing the Runnable interface may be
preferable to extending the Thread class:
 Extending the Thread class means that the
subclass cannot extend any other class,
whereas a class implementing the Runnable
interface
has this option.
 A class might only be interested in being
runnable, and therefore, inheriting the full
overhead of the Thread class would be
excessive.
Thread Transitions
Thread States

 Ready-to-run state
A thread starts life in the Ready-to-run state
 Running state
If a thread is in the Running state, it means
that the thread is currently executing
 Dead state
Once in this state, the thread cannot ever run
again
 Non-runnable states
A running thread can transit to one of the non-
runnable states, depending on the
circumstances. A thread remains in a non-
runnable state until a special transition occurs.
A thread does not go directly to the Running
state from a non-runnable state, but transits
first to the Ready-to-run state.
The non-runnable states can be characterized
as follows:
o Sleeping: The thread sleeps for a specified
amount of time
o Blocked for I/O: The thread waits for a
blocking operation to complete
o Blocked for join completion: The thread
awaits completion of another thread
o Waiting for notification: The thread awaits
notification from another thread
o Blocked for lock acquisition: The thread
waits to acquire the lock of an object

Methods
yield
public static void yield()
A call to the static method yield(), defined in
the Thread class, will cause the current thread
in the Running state to move to the Runnable
state, thus relinquishing the CPU. The thread
is then at the mercy of the thread scheduler
as to when it will run again. If there are no
threads waiting in the Ready-to-run state, this
thread continues execution. If there are other
threads in the Ready-to-run state, their
priorities determine which thread gets to
execute. The yield() method gives other
threads of the same priority a chance to run.
If there are no equal priority threads in the
“Runnable” state, then the yield is ignored.

Sleeping and Waking Up


Public static void
sleep(long millis)
The thread class contains a static method named
sleep() that causes the currently running thread to
pause its execution and transit to the Sleeping
state. The method does not relinquish any lock that
the thread might have. The thread will sleep for at
least the time specified in its argument, before
entering the runnable state where it takes its turn to
run again. If a thread is interrupted while sleeping,
it will throw an InterruptedException when it
awakes and gets to execute. The Thread class has
several overloaded versions of the sleep() method.

Joining
A thread invokes the join() method on another
thread in order to wait for the other thread to
complete its execution.
Consider a thread t1 invokes the method join() on
a thread t2. The join() call has no effect if thread t2
has already completed. If thread t2 is still alive,
then thread t1 transits to the Blocked-for-join-
completion state.
Below is a program showing how threads invoke
the overloaded thread join method.

You might also like