Child and Parent overridden methods Throws checked exception
Categories: Java 8(JDK1.8) Java Java Examples
package r4r.co.in;
import java.io.FileNotFoundException;
public class A {
public static void main(String[] args) throws Exception {
// Child and Parent overridden methods Throws checked exception (like
// FileNotFoundException)
AP ap = new B();
//ap.display();// compile time error we need handle checked exception
ap.display2();
ap.display3();
ap.display4();
AP a = new AP();
//a.display();// compile time error we need handle checked exception
a.display2();
a.display3();
a.display4();
B b = new B();
//b.display();// compile time error we need handle checked exception
b.display2();
b.display3();
b.display4();
// It will run and compile because RuntimeException not need to handle.So no
// need to throws or try catch here.
}
}
class AP {
//void display() throws FileNotFoundException {
//System.out.println("Display A1...");
//}
void display2() throws Exception {
System.out.println("Display 2 B1...");
}
void display3() throws FileNotFoundException {
System.out.println("Display 2 B1...");
}
void display4() {
System.out.println("Display 2 B1...");
}
}
//Here Its Runtime
class B extends AP {
// It will not compile it will throw exception ,because we can not throw parent
// (higher exception) we can throw only less or same exception
//void display() throws Exception {
//System.out.println("Display B1...");
//}
void display2() throws FileNotFoundException {
System.out.println("Display 2 B1...");
}
void display3() throws FileNotFoundException {
System.out.println("Display 2 B1...");
}
}