Child and Parent overridden methods Throws RuntimeException
Categories: Java 8(JDK1.8) Java Java Examples
package r4r.co.in;
public class A {
public static void main(String[] args) {
//Child and Parent overridden methods Throws RuntimeException (like ArrayIndexOutOfBoundsException)
AP ap = new B();
ap.display();
ap.display2();
ap.display3();
ap.display4();
AP a = new AP();
a.display();
a.display2();
a.display3();
a.display4();
B b=new B();
b.display();
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 IndexOutOfBoundsException{
System.out.println("Display A1...");
}
void display2() throws IndexOutOfBoundsException{
System.out.println("Display 2 B1...");
}
void display3() throws ArrayIndexOutOfBoundsException{
System.out.println("Display 2 B1...");
}
void display4() {
System.out.println("Display 2 B1...");
}
}
//Here Its Runtime
class B extends AP {
void display() throws IndexOutOfBoundsException {
System.out.println("Display B1...");
}
void display2() throws ArrayIndexOutOfBoundsException{
System.out.println("Display 2 B1...");
}
void display3() throws IndexOutOfBoundsException {
System.out.println("Display 2 B1...");
}
void display4() throws ArrayIndexOutOfBoundsException {
System.out.println("Display 2 B1...");
}
}