Salient Features of Java - 1
-
Upload
kfg20012003 -
Category
Documents
-
view
54 -
download
2
Transcript of Salient Features of Java - 1
Slide 1
Presented ByJanani JayarajYamini Udayakumar
Slide 2
Contents
Java-Features OOPS Concepts:
Encapsulation
Inheritance
Multithreading
Thread-Life Cycle
Synchronization
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)
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.
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
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
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
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
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)
Slide 10
Slide 11
Multi Threading
• Execution program with multiple threads in parallel
• Special form of multiprocessing
• Creating threads in java
Slide 12
New Ready to run Running Dead
blocked/waiting/sleeping
start() run() destroy()
notify()/notifyAll() Sleep()/wait()/blocked()
Thread created
Slide 13
New Ready to run Running Dead
blocked/waiting/sleeping
start() run() destroy()
notify()/notifyAll() Sleep()/wait()/blocked()
Thread ready to run
Slide 14
New Ready to run Running Dead
blocked/waiting/sleeping
start() run() destroy()
notify()/notifyAll() Sleep()/wait()/blocked()
Thread is running
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
Slide 16
New Ready to run Running Dead
blocked/waiting/sleeping
start() run() destroy()
notify()/notifyAll() Sleep()/wait()/blocked()
Thread terminated
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.
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
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
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();
}}
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();
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.
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.
Slide 24
Thank You
Slide 25
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
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
Slide 28
Multilevel Inheritance
Subclass is derived from a derived class then this mechanism is known as the multilevel inheritance.Eg: Grandfather, son, Grandson
Slide 29
Multilevel Inheritance
Super class
Intermediate class
Sub class
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
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