Professional Documents
Culture Documents
Thomas
Institute For Science and Technology
Trivandrum
• The methods that implement an interface must be declared public. Also, the type signature of the
implementing method must match exactly the type signature specified in the interface definition.
Example: A package that contains a class consisting of a method add() for adding two functions
In the first line the package name is
given “letmecalculate”
• In the above program we have imported the package as letmecalculate.Calculator, this only
imports the Calculator class.
• However if there are several classes inside package letmecalculate then we can import the
package like this, to use all the classes of this package
Default
catch Code can catch this exception (using catch) and handle catch(Exception obj)
it in some rational manner. System-generated {
exceptions are automatically thrown by the Java …//statements
runtime system }
throw To manually throw an exception, use the keyword throw throw throwableinstance
throws Any exception that is thrown out of a method must be throws ExceptionType
specified as such by a throws clause
finally Any code that absolutely must be executed after a try finally
block completes is put in a finally block. {
…..
}
Downloaded from Ktunotes.in
Q. Describe in detail about exception handling, try block and catch clause with the help of a suitable
Java program.
catch Code can catch this exception (using catch) catch(Exception obj)
and handle it in some rational manner. {
System-generated exceptions are …//statements
automatically thrown by the Java runtime }
system. The details of exception will be
stored in an Exception object obj.
In the above program, inside the try block, when a division by zero exception occurs, the catch
block for Arithmetic Exception will handle the situation. The details of exception will be stored
in object e.
Downloaded from Ktunotes.in
Q. Explain the use of finally using a java program
• When exceptions are thrown, execution in a method takes a rather abrupt, nonlinear path that alters the
normal flow through the method.
• Depending upon how the method is coded, it is even possible for an exception to
cause the method to return prematurely.This could be a problem in some methods.
• For example, if a method opens a file upon entry and closes it upon exit, then you
will not want the code that closes the file to be bypassed by the exception-handling mechanism.
• The finally keyword is designed to address this contingency.
• finally creates a block of code that will be executed after a try/catch block has
completed and before the code following the try/catch block.
• The finally block will execute whether or not an exception is thrown.
• If an exception is thrown, the finally block will execute even if no catch statement
matches the exception.
• Any time a method is about to return to the caller from inside a try/catch block, via an uncaught
exception or an explicit return statement, the finally clause is also executed just before the method
returns.
throw
• Used to throw an exception explicitly(manually)
• Syntax:
throw ThrowableInstance;.
Here, ThrowableInstance must be an object of
type Throwable or a subclass of Throwable.
• The flow of execution stops immediately after the throw
statement; any subsequent statements are not executed.
• The nearest enclosing try block is inspected to see if it
has a catch statement that matches the type of
exception. If it does find a match, control is transferred
to that statement. If not, then the next enclosing try
statement is inspected, and so on.
• If no matching catch is found, then the default exception
handler halts the program and prints the stack trace.
The above code gives different results. This is because string literals are stored in the string constant
pool, and both s1 and s2 shares the same reference. Therefore, s1 == s2 returns true.
import java.io.*;
class Persist{
public static void main(String args[]){
try{
Employee emp1 =new Employee(20110,"John");
Employee emp2 =new Employee(22110,"Jerry");
Employee emp3 =new Employee(20120,"Sam");
FileOutputStream fout=new FileOutputStream("output.txt");
ObjectOutputStream out=new ObjectOutputStream(fout);
out.writeObject(emp1);
out.writeObject(emp2);
out.writeObject(emp3);
out.flush();
out.close();
System.out.println("Serialization is been successfully executed");
}
catch(Exception e){
System.out.println(e);}
}
}
import java.io.*;
class Depersist{
public static void main(String args[]){
try{
ObjectInputStream in=new ObjectInputStream(new FileInputStream("output.txt"));
Employee e1=(Employee)in.readObject();
Employee e2=(Employee)in.readObject();
Employee e3=(Employee)in.readObject();
System.out.println(e1.id+" "+e1.name);
System.out.println(e2.id+" "+e2.name); In the above programs 3 objects of
System.out.println(e3.id+" "+e3.name); Employee class has been converted into
in.close(); byte stream and stored in a file named
}
Output.txt during serialization.
catch(Exception e){
Later during deserialization these byte
System.out.println(e);} streams are again restored into objects of
} Employee class.
} Downloaded from Ktunotes.in
Q. Explain any 2 Collection interfaces
1. Collection interface
• The Collection interface is the foundation upon which the Collections Framework is built because it
must be implemented by any class that defines a collection.
• Collection is a generic interface that has this declaration:
interface Collection<E>
Here, E specifies the type of objects that the collection will hold.
• Collection implements the Iterable interface. This means that all collections can be cycled through by
use of the for-each style for loop.
• Some methods in Collection interface can throw
• An UnsupportedOperationException. this occurs if a collection cannot be modified.
• A ClassCastException is generated when one object is incompatible with another, such as when an attempt is made to
add an incompatible object to a collection.
• A NullPointerException is thrown if an attempt is made to store a null object and null elements are not allowed in the
collection.
• An IllegalArgumentException is thrown if an invalid argument is used.
• An IllegalStateException is thrown if an attempt is made to add an element to a fixed-length collection that is full.
• When two or more threads need access to a shared resource, they need some way to ensure that
the resource will be used by only one thread at a time. The process by which this is achieved is
called synchronization.
• Key to synchronization is the concept of the monitor (also called a semaphore). A monitor is an
object that is used as a mutually exclusive lock, or mutex. Only one thread can own a monitor at a
given time. When a thread acquires a lock, it is said to have entered the monitor.
• All other threads attempting to enter the locked monitor will be suspended until the first thread
exits the monitor. These other threads are said to be waiting for the monitor. A thread that owns a
monitor can reenter the same monitor if it so desires.
• To enter an object’s monitor, just call a method that has been modified with the synchronized
keyword.
• While a thread is inside a synchronized method, all other threads that try to call it (or any other
synchronized method) on the same instance have to wait.
• To exit the monitor and relinquish control of the object to the next waiting thread, the owner of the
monitor simply returns from the synchronized method.