Example of Subclass overridden Method declaring Checked Exception
import java.io.*; class Super { void show() { System.out.println("parent class"); } } public class Sub extends Super { void show() throws IOException //Compile time error { System.out.println("parent class"); } public static void main( String[] args ) { Super s=new Sub(); s.show(); } }
As the method show() doesn’t throws any exception while in Super class, hence its overriden version can also not throw any checked exception.
Example of Subclass overriden Method declaring Unchecked Exception:
import java.io.*; class Super { void show(){ System.out.println("parent class"); } } public class Sub extends Super { void show() throws ArrayIndexOutOfBoundsException //Correct { System.out.println("child class"); } public static void main(String[] args) { Super s=new Sub(); s.show(); } }
Because ArrayIndexOutOfBoundsException is an unchecked exception hence, overridden show() method can throw it.
More about Overridden Methods and Exceptions:
If Super class method throws an exception, then Subclass overridden method can throw the same exception or no exception, but must not throw parent exception of the exception thrown by Super class method.
It means, if Super class method throws object of NullPointerException class, then Subclass method can either throw same exception, or can throw no exception, but it can never throw object of Exception class (parent of NullPointerException class).
Example of Subclass overridden method with same Exception
import java.io.*; class Super { void show() throws Exception { System.out.println("parent class"); } } public class Sub extends Super { void show() throws Exception //Correct { System.out.println("child class"); } public static void main(String[] args) { try { Super s=new Sub(); s.show(); } catch(Exception e){} } }
Example of Subclass overriden method with no Exception:
import java.io.*; class Super { void show() throws Exception { System.out.println("parent class"); } } public class Sub extends Super { void show() //Correct { System.out.println("child class"); } public static void main(String[] args) { try { Super s=new Sub(); s.show(); } catch(Exception e){} } }
Example of Subclass overriden method with parent Exception:
import java.io.*; class Super { void show() throws ArithmeticException { System.out.println("parent class"); } } public class Sub extends Super { void show() throws Exception //Cmpile time Error { System.out.println("child class"); } public static void main(String[] args) { try { Super s=new Sub(); s.show(); } catch(Exception e){} } }
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…