Top Banner
Slide 1 Presented By Janani Jayaraj Yamini Udayakumar
31

Salient Features of Java - 1

Mar 04, 2015

Download

Documents

kfg20012003
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Salient Features of Java - 1

Slide 1

Presented ByJanani JayarajYamini Udayakumar

Page 2: Salient Features of Java - 1

Slide 2

Contents

Java-Features OOPS Concepts:

Encapsulation

Inheritance

Multithreading

Thread-Life Cycle

Synchronization

Page 3: Salient Features of Java - 1

Slide 3

Features Of Java

• Platform Independence (Write Once run any where)

• Object Oriented (No coding outside class )

• Compiler/Interpreter Combo (Extensive code checking)

• Robust (easy )

• Several dangerous features of C & C++ eliminated

• Automatic Memory Management (memory management by JVM)

• Security

• Dynamic Binding (linking of data & methods at runtime)

• Good Performance (just in-time Compilation)

• Threading (Multiprocessing)

• Built-in Networking(Internet Communications)

Page 4: Salient Features of Java - 1

Slide 4

Encapsulation

• Encapsulation is the process of hiding all the details of an entity that do not contribute to its essential characteristics

For example,

a procedure is a type of encapsulation because it combines a series of computer instructions.

Page 5: Salient Features of Java - 1

Slide 5

ENCAPSULATION

• Encapsulation hides the implementation of an abstraction from its users

• Encapsulation is often reffered to as Information hiding

• Only the interface to an abstraction should be known to its clients

• How that interface is implemented is hidden for the client

Page 6: Salient Features of Java - 1

Slide 6

The only way to access the state of the object should be via the methods provided in its interface

The methods encapsulates the states

Page 7: Salient Features of Java - 1

Slide 7

• Inheritance is the creation of one class(sub class) by extending another class(super class) so that instances of the new class automatically inherit the fields and methods of its parent class.

• Example

Inheritance

Page 8: Salient Features of Java - 1

Slide 8

class Account {Account(){System.out.println("Parent Class");} }Class Savings extends Account{Savings(){System.out.println("In Saving Child Class");} }Class Current extends Account{Current(){System.out.println("In Current Child Class");}}

public static void main(String arg[]){Account child=new Saving();Account child1=new Current();}}

Super class

Sub classes

Page 9: Salient Features of Java - 1

Slide 9

Types Of Inheritance

Single Level Inheritance(only one super class) Multilevel Inheritance(derived from derived class) Hierarchical Inheritance(one super class many sub class) Multiple Inheritance(several super classes Hybrid Inheritance(more than two types)

Page 10: Salient Features of Java - 1

Slide 10

Page 11: Salient Features of Java - 1

Slide 11

Multi Threading

• Execution program with multiple threads in parallel

• Special form of multiprocessing

• Creating threads in java

Page 12: Salient Features of Java - 1

Slide 12

New Ready to run Running Dead

blocked/waiting/sleeping

start() run() destroy()

notify()/notifyAll() Sleep()/wait()/blocked()

Thread created

Page 13: Salient Features of Java - 1

Slide 13

New Ready to run Running Dead

blocked/waiting/sleeping

start() run() destroy()

notify()/notifyAll() Sleep()/wait()/blocked()

Thread ready to run

Page 14: Salient Features of Java - 1

Slide 14

New Ready to run Running Dead

blocked/waiting/sleeping

start() run() destroy()

notify()/notifyAll() Sleep()/wait()/blocked()

Thread is running

Page 15: Salient Features of Java - 1

Slide 15

New Ready to run Running Dead

blocked/waiting/sleeping

start() run() destroy()

notify()/notifyAll() sleep()/wait()/blocked()

Thread in blocked , waiting, sleep state

Page 16: Salient Features of Java - 1

Slide 16

New Ready to run Running Dead

blocked/waiting/sleeping

start() run() destroy()

notify()/notifyAll() Sleep()/wait()/blocked()

Thread terminated

Page 17: Salient Features of Java - 1

Slide 17

Thread States

New:

It is when the thread object is created Runnable State:

• When start() method is called on a thread it goes into a ready to run state i.e. Runnable

• When interrupt() method is used on a running thread, it comes into a ready state

• There can be many threads in a runnable state.

• A thread can again come to runnable state after running or coming back

from sleeping, waiting or blocked state.

Page 18: Salient Features of Java - 1

Slide 18

Thread States (Cont…)

Sleeping state:

• Sleep() is a static method and executed on currently executing thread

• It is used to delay execution for a period of time.CPU is not used by that thread for that mentioned period of time

• Even if a thread goes to sleep, it doesn’t release the locks Blocked State:

• If a thread is waiting for an indefinite time until I/o takes place, then a then the thread is said to be in blocked state

Dead:

• When a thread completes its run() method it is said to be dead

• A dead thread is always dead

Page 19: Salient Features of Java - 1

Slide 19

Thread CreationThread can be created in 2 ways:

• Extending Thread class and overriding the public void run() method

• Implementing Runnable Interface and adding the code to be executed by the thread in void run() method.

A runnable object is created & it is passed as an arguments to the thread class constructor to create the Thread

 

Page 20: Salient Features of Java - 1

Slide 20

Creating Threads

• Runnable Interface:• Create Object Implementing Runnable Interface

• Pass it to thread object via Thread constructor

Example• Public class T implements Runnable {

• Public void run()

• { //working of thread}}

public class Thread{

Public static void main(String args[]);

{

Thread thread=new Thread(new NewThread());

Thread. start();

}}

Page 21: Salient Features of Java - 1

Slide 21

Creating Threads(Cont…)

• Extends Thread Class

public class T extends thread{

public void run(){//Working of thread}

}

T t=new T();

t.start();

Page 22: Salient Features of Java - 1

Slide 22

Synchronization

1. Two or more threads accessing the same data simultaneously may lead to loss of data integrity.

2. In order to avoid this java uses the concept of monitor. A monitor is an object used as a mutually exclusive lock.

3. At a time only one thread can access the Monitor. A second thread cannot enter the monitor until the first comes out. Till such time the other thread is said to be waiting .

4. The keyword Synchronized is use in the code to enable synchronization and it can be used along with a method.

Page 23: Salient Features of Java - 1

Slide 23

InterProcess Communication

• wait() tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify().

• notify() wakes up a thread that called wait() on the same object.

• notifyAll() wakes up all the threads that called wait() on the same object. One of the threads will be granted access.

Page 24: Salient Features of Java - 1

Slide 24

Thank You

Page 25: Salient Features of Java - 1

Slide 25

Page 26: Salient Features of Java - 1

Slide 26

Single Inheritance

• Single Inheritance – Derived class has only one direct base class – Creates “simple” hierarchy of classes - trees – One to one inheritance of members – Specializes a base class

Page 27: Salient Features of Java - 1

Slide 27

Single Inheritance

• Single Inheritance

– Derived class has only one direct base class

– Creates “simple” hierarchy of classes - trees

– One to one inheritance of members

– Specializes a base class

Page 28: Salient Features of Java - 1

Slide 28

Multilevel Inheritance

Subclass is derived from a derived class then this mechanism is known as the multilevel inheritance.Eg: Grandfather, son, Grandson

Page 29: Salient Features of Java - 1

Slide 29

Multilevel Inheritance

Super class

Intermediate class

Sub class

Page 30: Salient Features of Java - 1

Slide 30

Hierarchical Inheritance

It is that having one super class and many sub classesCertain features of one level can be shared by other levelsEg:Saving account & current account acquires the properties of Account class

Page 31: Salient Features of Java - 1

Slide 31

Multiple & Hybrid Inheritance

• Multiple Inheritance:

Java does not support Multiple Inheritance

To achieve this ,we use the concept of Interface

• Hybrid Inheritance:

Java does not support Hybrid Inheritance since it is a composition of Multilevel & Multiple Inheritance