MODULE III MR-22 Java Intefaces Exceptiona Handling and Multithearding-1
MODULE III MR-22 Java Intefaces Exceptiona Handling and Multithearding-1
Part-A
Syntax :
interface <interface-name> {
void method-name();
}
public class <interface-name> implements <user-defined variable
name>
{
void method-name()
{
// inputs and outputs statements;
}
public static void main (String args[])
{
class-name objec-name = new class-name();
// call or display to method
}
}
Example 1: Write a java program how to implement interface and
display message are “Hello? How are U?”.
import java.util.*;
inteface mrec { // To create or applying a intefaces
void cse(); // user defined method declarations
}
public class mrec implements csedept // To implementing Interface
{
public static void print()
{
System.out.println (“ Hello? How are U?”);
}
public static void main (String args[])
{
mrec m=new mrec(); // To create a object
m.cse();
}
}
Output:
import java.util.*;
interface Drawable { // To create or applying Interface
void draw();
}
class Drawable implements rect { // To create rectangle class
Implementing Intefaces
void draw()
{
System.out.println (“ Drawing a Rectangle”);
}
}
class Drawable implements cir { // To create circle class implementing
Interfaces
void draw()
{
System.out.println (“Drawing a Circle”);
}
}
public class cse {
Public static void main(String args[])
{
Drawable d = new Drawable(); // To create a object
d.draw();
}
}
Multiple Interfaces in Java
Multiple Interfaces support to java in place of Multiple Inheritance.
import java.util.*;
interface Flyable { // To Create/applying a Interfac1 1
void fly();
}
interface Eatable { // To Create /applying Interface 2
void eat();
}
class Bird implements Flyable, Eatable // To implementing Interfaces
1 and 2
{
public void fly()
{
System.out.println("Bird flying");
}
public void eat()
{
System.out.println("Bird eats");
}
}
public class cse
{
public static void main(String[] args)
{
Bird b = new Bird();
b.eat();
b.fly();
}
}
Output:
Bird eats
Bird flying
Difference between abstract class and interface :
3) Abstract class can have final, non- Interface has only static and final variables.
final, static and non-static variables.
4) Abstract class can provide the Interface can't provide the implementation
implementation of interface. of abstract class.
6) An abstract class can extend another An interface can extend another Java interface
Java class and implement multiple Java only.
interfaces.
8) A Java abstract class can have class Members of a Java interface are public
members like private, protected, etc. by default.
9) Example: Example:
public abstract class Shape public interface Drawable
{ {
public abstract void draw(); void draw();
} }
Example 3 : Write a java program how to create interfaces using a
abstract class.
Variables in Interface
The interface contains the static, final, and default variables the
automatically adds as a public.
Extending Interfaces
The interface that extends another interface has its own members and
all the members defined in its parent interface.
}
}
Types of io stream :
Byte Stream
Character Stream
1. Byte Streams
Byte stream is used to read and write a single byte (8 bits) of data.
1 int available()
It returns the number of bytes that can be read from the input stream.
2 int read()
It reads the next byte from the input stream.
3 int read(byte[] b)
It reads a chunk of bytes from the input stream and store them in its byte
array, b.
4 void close()
It closes the input stream and also frees any resources connected with this
input stream.
B. Output Stream class
The OutputStream class has defined as an abstract class, and it has the
following methods which have implemented by its concrete classes.
S.No
. Method with Description
1 void write(int n)
It writes byte(contained in an int) to the output stream.
2 void write(byte[] b)
It writes a whole byte array(b) to the output stream.
3 void flush()
It flushes the output steam by forcing out buffered bytes to be written
out.
4 void close()
It closes the output stream and also frees any resources connected with
this output stream.
import java.io.*;
public class ReadingDemo {
2. Character Streams
A. Reader Class :
The Reader class has defined as an abstract class, and it has the following
methods which have implemented by its concrete classes.
S.No. Method with Description
1 int read()
It reads the next character from the input stream.
5 String readLine()
It reads a line of text. A line is considered to be terminated by any oneof a lin
a carriage return ('\r').
6 boolean ready()
It tells whether the stream is ready to be read.
7 void close()
It closes the input stream and also frees any resources connected with this inp
import java.io.*;
public class ReadingDemo {
B. Writer class :
The Writer class has defined as an abstract class, and it has the following
methods which have implemented by its concrete classes.
S.No. Method with Description
1 void flush()
It flushes the output steam by forcing out buffered bytes to be
written out.
4 void write(int c)
It writes single character.
7 Writer append(char c)
It appends the specified character to the writer.
10 void close()
It closes the output stream and also frees any resources connected
with this output stream.
import java.io.*;
public class WritingDemo {
public static void main(String[] args) throws IOException
{
try {
out.write(msg);
System.out.println("Writing done!!!");
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
out.close();
}
}
}
MODULE-III
Part-B
Exceptions Handling & Multi Threading in Java
The Exception Handling in Java is one of the powerful mechanism to
handle the runtime errors so that the normal flow of the application
can be maintained.
1) Checked Exception:
The classes that directly inherit the Throwable class except
RuntimeException and Error are known as checked exceptions.
2) Unchecked Exception:
The classes that inherit the RuntimeException are known as
unchecked exceptions.
Example:-
ArithmeticException,NullPointerException,OutofMemory,ArrayInde
xOutOfBoundsException, VirtualMachineError, etc.
Keywords Description
finally The "finally" block is used to execute the necessary code of the
program.
It is executed whether an exception is handled or not.
throw The "throw" keyword is used to throw an single exception.
import java.lang.*;
public class TCatchCse {
OUTPUT
java.lang.ArithmeticException: / by zero
rest of the code Hava Nice Day!
Example 2: Write a java program how to create exception handling
import java.lang.*;
public class csed {
public static void main(String[] args) throw IOExcption
{
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e)
{
System.out.println("Something went wrong.");
}
finally
{
System.out.println("The 'try catch' is finished.");
}
}
}
while the outer try block can handle the ArithemeticException (division
by zero).
1. ....
2. //main or outer try block
3. try
4. {
5. statement 1;
6. statement 2;
7. //try catch inner block within another try block
8. try
9. {
10. statement 3;
11. statement 4;
12. //try catch inner another block within nested try block
13. try
14. {
15. statement 5;
16. statement 6;
17. }
18. catch(Exception e2) ;
19. {
20. //exception message-1
21. }
22. }
23. catch(Exception e1)
24. {
25. //exception message-2 ;
26. }
27. }
28. //catch block of parent (outer) try block
29. catch(Exception e3)
30. {
31. //exception message-3 ;
32. }
33. ....
if(age<18)
try
catch(Exception e)
{
System.out.println ("you are not an adult!");
else
canVote(20);
canVote(10);
void start();
(OR)
import java.io.*;
import java.lang.*;
public class Main extends Thread {
public static void main(String[] args)
{
Main thread = new Main();
thread.start();
System.out.println("This code is outside of the thread");
}
public void run()
{
System.out.println("This code is running in a thread");
}
}
import java.io.*;
import java.lang.*;
public class cse implements Runnable
{
public void run()
{
System.out.println("Thread has ended");
}
OUTPUT :
import java.io.*;
import java.lang.*;
Public class RunnableDemo implements Runnable {
public Thread t;
public String threadName;
RunnableDemo( String name) {
threadName = name;
System.out.println("Creating " + threadName );
}