Professional Documents
Culture Documents
2
Objectives
• Abstract classes
• Interface
• Exception Handling
• Collection Framework
• File IO Basics
• Multithreading Basics
• JDBC
3
Abstract Class
• Abstract class is which doesn’t have any existence. So we can’t
instantiate abstract class.
• If any method within a class is abstract then the class should be
abstract.
• Abstract class can contain abstract method as well as concrete
method.
• The class which inherits abstract class should implement all the
abstract methods, if not then we have to make child class as
abstract class.
• Don’t use abstract & final combination
4
Example :
7
Interface
• Valid Combination:
• Class extends Class
• Class implements interface
• Interface extends interface
8
String Class
• It’s a built in class from java.lang package.
• It is a first class object (This doesn’t require
new keyword).
String s =“Hello”;
• It is immutable (can’t modify the content of
the existing string)
9
String Buffer
• It’s a built in class from java.lang package.
• String buffer class is mutable
• It is synchronized.
10
Wrapper Classes
• Wrapper class in java provides the mechanism to convert
primitive into object and object into primitive.
Primitive Type Wrapper class
boolean Boolean
int Integer
long Long
double Double
11
Auto Boxing & Auto Unboxing
• Auto Boxing:
int i = 10;
// Integer obj = new Integer(i); ->Before java 1.5
Integer obj = i; ->From java 1.5
• Auto UnBoxing:
//Integer obj = new Integer(10);
//int i = obj.intValue(); -> Before java 1.5
Int i = obj; From 1.5 (Auto Unwrapping)
12
Exception Handling
• The exception handling in java is one of the
powerful mechanism to handle the runtime errors so
that normal flow of the application can be maintained..
Ex:
• Number format exception
• Array Index out of bound exception
• Arithmetic exception
• Null pointer exception
• Custom Exception
13
Exception Hierarchy
14
Exception Hierarchy
• Throwable (super class)
15
Five keywords:
• try
• catch
• finally
• throw
• throws
16
try {
// The code which may generate exception put that
code in try block
}
catch (Exception e) {
// To handle those exception we need catch block
}
finally{
//Finally block get executed always
}
import java.io.*;
class M{
void method()throws IOException{
throw new IOException("device error");
}
}
public class Testthrows2{
public static void main(String args[]){
try{
M m=new M();
m.method();
}catch(Exception e){System.out.println("exception handled");}
}
} 20
User Defined Exception
• If you are creating your own Exception that is
known as custom exception or user-defined
exception.
• Java custom exceptions are used to customize
the exception according to user need.
• E.g
EmployeeNotFoundException
InvalidAgeException
21
class InvalidAgeException extends Exception{
InvalidAgeException(String s){
super(s);
}
}
class TestCustomException{
static void validate(int age)throws InvalidAgeException{
if(age<18)
throw new InvalidAgeException("not valid");
else
System.out.println("welcome to vote");
}
public static void main(String args[]){
try{
validate(13);
}catch(Exception m)
{ System.out.println("Exception occured: "+m);} 22
}}
Collection
• Collection is a data structure which store and
manipulate the group of objects (It doesn’t
store primitive data type).
23
Collection Hierarchy
24
List
Features of List
• May contain duplicate element
• It is an ordered collection
• Implementation classes are Linked List, Vector &
Array List
25
List
• Linked List
• To add the value
list.addFirst(), list.addLast (), list.add(1, 33)
• To retrieve the value
list.getFirst(), list.getLast() , list.get(1)
• To remove the value
list.removeFirst(), list.removeLast(), list.remove(1))
26
Set
• Features of Set
• Can’t contain duplicate element
• It is an unordered collection
• Implementation classes are HashSet and
TreeSet
27
Map
• Features of Map
• Map contains key and value pairs
• Key must be unique
• value can be different
• Implementation classes are HashTable,
LinkedHash Map, & Tree Map
29
Files and I/O
• The java.io package contains nearly every class you might
ever need to perform input and output (I/O) in Java.
• Stream
A stream can be defined as a sequence of data.
There are two kinds of Streams
• InPutStream: The InputStream is used to read data from a
source.
• OutPutStream: the OutputStream is used for writing data
to a destination.
30
Byte Streams
• Java byte streams are used to perform input
and output of 8-bit bytes.
• FileInputStream and FileOutputStream.
31
import java.io.*;
class Demo1{
public static void main(String args[])throws Exception{
FileInputStream fin=new FileInputStream("C.java");
FileOutputStream fout=new FileOutputStream("M.java");
int i=0;
while((i=fin.read())!=-1){
fout.write((byte)i);
}
fin.close();
}
}
32
Character Streams
• Java Character streams are used to perform
input and output for 16-bit unicode.
• FileReader and FileWriter.
33
Multithreading
• Multitasking
• Multitasking is a process of executing multiple tasks simultaneously.
}
}
2) Class Demo extends Thread{
Public void run(){
}
}
35
Thread Lifecycle
36
Thread Lifecycle
• start(): Ready to Run
• States of thread
• Born -> Ready to Start - > Running - > Sleep - > Dead state
37
class TestSleepMethod extends Thread{
39
• //example of java synchronized method
class Table{
synchronized void printTable(int n){//synchronized method
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
} } }
class MyThread1 extends Thread{
Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5); } } 40
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
public class TestSynchronization{
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
41
}
Inter thread Communication
• Inter-thread communication is all about allowing
synchronized threads to communicate with each other.
wait()
notify()
notifyAll()
42
Inter thread Communication
• wait():wait will pause the execution until
another threads gives notification.
• notify(): notify will give notification to single
thread
• notifyAll():notify will give notification to all
the threads which are in wait condition
43
JDBC (Java Database Connectivity)
• Java JDBC is a java API to connect and execute
query with the database.
• JDBC API uses jdbc drivers to connect with the
database.
44
JDBC Drivers
• Type I (JDBC –ODBC bridge)
• Type II (Native API Driver)
• Type III ( Network Protocol Driver )
• Type IV (Thin Driver – Pure Java Driver)
45
Thin driver
• The thin driver converts JDBC calls directly into the
vendor-specific database protocol.
• That is why it is known as thin driver.
• It is fully written in Java language.
48
import java.sql.*;
class OracleCon{
public static void main(String args[]){
try{
//step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");