
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Is parent child hierarchy important on throws while overriding in Java?
When you try to handle an exception (checked) thrown by a particular method, you need to catch it using the Exception class or super class of the Exception occurred.
In the same way while overriding the method of a super class, if it throws an exception −
The method in the sub-class should throw the same exception or its sub type.
The method in the sub-class should not throw its super type.
You can override it without throwing any exception.
When you have three classes named Demo, SuperTest and, Super in (hierarchical) inheritance, if Demo and SuperTest have a method named sample().
Example
class Demo { public void sample() throws ArrayIndexOutOfBoundsException { System.out.println("sample() method of the Demo class"); } } class SuperTest extends Demo { public void sample() throws IndexOutOfBoundsException { System.out.println("sample() method of the SuperTest class"); } } public class Test extends SuperTest { public static void main(String args[]) { Demo obj = new SuperTest(); try { obj.sample(); }catch (ArrayIndexOutOfBoundsException ex) { System.out.println("Exception"); } } }
Output
sample() method of the SuperTest class
If the class with which you catch an exception is not same or, exception or, super class of the raised exception, you will get a compile time error.
In the same way, while overriding a method the exception thrown should be same or, super class of the exception thrown by the overridden method otherwise a compile time error occurs.
Example
import java.io.IOException; import java.io.EOFException; class Demo { public void sample() throws IOException { System.out.println("sample() method of the Demo class"); } } class SuperTest extends Demo { public void sample() throws EOFException { System.out.println("sample() method of the SuperTest class"); } } public class Test extends SuperTest { public static void main(String args[]) { Demo obj = new SuperTest(); try { obj.sample(); }catch (EOFException ex){ System.out.println("Exception"); } } }
Output
Test.java:12: error: sample() in SuperTest cannot override sample() in Demo public void sample() throws IOException { ^ overridden method does not throw IOException 1 error D:\>javac Test.java Test.java:20: error: unreported exception IOException; must be caught or declared to be thrown obj.sample(); ^ 1 error
- Related Articles
- Guidelines to follow in while overriding a method that throws an exception in java?
- What are Java parent and child classes in Java?
- Can we change an exception of a method with throws clause from unchecked to checked while overriding it in java?
- Parent and Child classes having same data member in Java
- jQuery parent > child Selector
- What are the rules need to follow when overriding a method that throws an exception in Java?
- Why static methods of parent class gets hidden in child class in java?
- If a method in parent class “throws Exception”, can we remove it in overridden method in java?
- While overriding can the subclass choose not to throw an exception in java?
- What are the rules on method overriding in Java?
- Process vs Parent Process vs Child Process
- Parent-Child Interaction Therapy: Meaning And Application
- Method overriding in Java
- Overriding in Java programming
- Calculation in parent and child process using fork() in C++
