Create a Java program to demonstrate method overriding with a subclass that overrides a method and throws a subclass exception
Class hierarchy with a Parent class and a Child class that extends Parent. Both classes have a display method, but in the Child class, you've overridden the display method. The display method in the Parent class declares that it can throw an Exception, and the overridden display method in the Child class declares that it can throw a RuntimeException. Here's what happens when you run the main method:
class Parent { void display() throws Exception { System.out.println("Parent's display method"); } } class Child extends Parent { @Override void display() throws RuntimeException { System.out.println("Child's display method"); } } public class Main { public static void main(String[] args) { Parent parent = new Child(); try{ parent.display(); } catch (Exception e) { e.printStackTrace(); } } }
Child's display method
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions