java_unit_3
java_unit_3
• There can be only abstract methods in the Java interface, not method body.
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
A7 obj = new A7();
obj.print();
obj.show();
}
}
Interface inheritance
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class TestInterface4 implements Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
TestInterface4 obj = new TestInterface4();
obj.print();
obj.show();
}
}
Default Method in Interface
interface Drawable{
void draw();
default void msg(){System.out.println("default method");}
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class TestInterfaceDefault{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
d.msg();
}}
Static Method in Interface
interface Drawable{
void draw();
static int cube(int x){return x*x*x;}
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class TestInterfaceStatic{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
System.out.println(Drawable.cube(3));
}}
Difference Between Interface and Abstract Class in Java
interface Vehicle {
class Engine {
void start() {
System.out.println("Engine started");
}
}
}
No import required
Uses mypackage.Hello instead of Hello
java
Importing a Package
Instead of using fully qualified names, we can import a package using the import statement.
The core advantage of exception handling is to maintain the normal flow of the
application. An exception normally disrupts the normal flow of the application; that
is why we need to handle exceptions.
statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception occurs
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;
Suppose there are 10 statements in a Java program and an exception
occurs at statement 5; the rest of the code will not be executed, i.e.,
statements 6 to 10 will not be executed. However, when we perform
exception handling, the rest of the statements will be executed. That is
why we use exception handling in Java.
Types of Java Exceptions
Checked Exception: Checked exceptions are checked at compile-time.
At a time only one exception occurs and at a time only one catch block is executed.
All catch blocks must be ordered from most specific to most general, i.e. catch for
ArithmeticException must come before catch for Exception.
public static void main(String[] args) {
try{
int a[]=new int[5];
a[5]=30/0; }
catch(ArithmeticException e) {
System.out.println("Arithmetic Exception occurs"); }
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBounds Exception
occurs"); }
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
public static void main(String[] args) {
try{
int a[]=new int[5];
a[5]=30/0; //Only one catch will get execute
System.out.println(a[10]) }//
catch(ArithmeticException e) {
System.out.println("Arithmetic Exception occurs"); }
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBounds Exception
occurs"); }
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
public static void main(String[] args) {
try{
String s=null; //As no exception is written parent class Exception will be called
System.out.println(s.length());
catch(ArithmeticException e) {
System.out.println("Arithmetic Exception occurs"); }
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBounds Exception occurs"); }
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
Java Nested try Example
} }
// to handle ArithmeticException
catch (ArithmeticException e) {
System.out.println("Arithmetic exception");
System.out.println("inner try block 1");
}
}
// to handle ArrayIndexOutOfBoundsException
catch (ArrayIndexOutOfBoundsException e4) {
System.out.print(e4);
System.out.println(" outer (main) try block");
}
catch (Exception e5) {
System.out.print("Exception");
System.out.println(" handled in main try-block");
}
}
}
finally block
class TestFinallyBlock {
public static void main(String args[]){
try{
//below code do not throw any exception
int data=25/5;
System.out.println(data);
}
//catch won't be executed
catch(NullPointerException e){
System.out.println(e);
}
//executed regardless of exception occurred or not
finally {
System.out.println("finally block is always executed");
}
System.out.println("rest of phe code...");
}
}
When an exception occurr but not
handled by the catch block
class TestFinallyBlock {
public static void main(String args[]){
try{
//below code do not throw any exception
int data=25/0;
System.out.println(data);
}
//catch won't be executed
catch(NullPointerException e){
System.out.println(e);
}
//executed regardless of exception occurred or not
finally {
System.out.println("finally block is always executed");
}
System.out.println("rest of phe code...");
}
}
When an exception occurs and is
handled by the catch block
class TestFinallyBlock {
public static void main(String args[]){
try{
//below code do not throw any exception
int data=25/0;
System.out.println(data);
}
//catch won't be executed
catch(ArithmeticException e){
System.out.println(e);
}
//executed regardless of exception occurred or not
finally {
System.out.println("finally block is always executed");
}
System.out.println("rest of phe code...");
}
}
throw Exception
The Java throw keyword is used to throw an exception explicitly.
Throwing Unchecked Exception
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");}
System.out.println("normal flow...");
}
}
48
Assignment Questions
1. Explain the differences between interfaces and abstract classes with proper examples in
Java.Write a program that demonstrates both interface and abstract class usage.
2. Define an interface Vehicle with methods startEngine() and stopEngine().
Implement this interface in two classes: Car and Bike.
Write a Main class that demonstrates polymorphism using interface references.
3. Define an interface Bank with a method getInterestRate().
Create two classes, SBI and HDFC, that implement this interface with different interest
rates. In the Main class, create an interface reference to access different
implementations.
PIMPRI CHINCHWAD UNIVERSITY
49
Assignment Questions
4. Define an interface Animal with method makeSound().
Extend this interface in another interface Pet which includes a method play().
Implement Pet in a class Dog and provide implementations for both methods.
5. Write a program where an inner class implements an interface.
Define an interface Greeting with a method sayHello().
Create an outer class Person that has an inner class Friendly which
implements Greeting. Access the inner class method using an instance of
the outer class.
Assignment Questions
6. Define a package mypackage and create a class Calculator inside it with methods
add(), subtract(), multiply(), and divide(). Write a separate Java program to import
and use this package.
7. Write a Java program that demonstrates the use of the java.util package.
Use ArrayList and HashMap to store and retrieve data.
8. Explain the Exception Hierarchy in Java with a diagram.
Write a Java program that demonstrates handling multiple exceptions using try,
catch, and finally.
Assignment Questions
9. Write a Java program that demonstrates both Checked and Unchecked Exceptions.
Use FileNotFoundException (Checked Exception)
Use ArithmeticException (Unchecked Exception)
Handle them using try, catch, and finally.