Observation Book Programs 35
Observation Book Programs 35
Program on interface
interface Pet{
public void test();
}
class Dog implements Pet{
public void test(){
System.out.println("Interface Method Implemented");
}
public static void main(String args[]){
Pet p = new Dog();
p.test();
}
}
Output:Hello
28. Program on package using fully qualified name
//save by A.java
package pack;
public class A{
public void msg(){ System.out.println("Hello"); } }
//save by B.java
package mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A();//using fully qualified name
obj.msg();
} }
Output:
34. Program on exception handling-throws block
public class TestThrows {
//defining a method
public static int divideNum(int m, int n) throws ArithmeticException {
int div = m / n;
return div;
}
//main method
public static void main(String[] args) {
TestThrows obj = new TestThrows();
try {
System.out.println(obj.divideNum(45, 0));
}
catch (ArithmeticException e){
System.out.println("\nNumber cannot be divided by 0");
}
Output:
35. Program on user defined exception
public class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}