aspose file tools
The moose likes Beginning Java and the fly likes overriding in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "overriding in java" Watch "overriding in java" New topic

overriding in java

Suresh KumarPandey
Ranch Hand

Joined: Sep 08, 2012
Posts: 46

Why the foo method of class X is not throwing a a compile error because according to the override rule ,if the superclass method has not declared exception ,the subclass method can't declare a new exception
Ivan Jozsef Balazs

Joined: May 22, 2012
Posts: 919
RuntimeException is not a checked one.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14483

And that rule is only valid for checked exceptions.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Shaddy Khan

Joined: Jul 26, 2013
Posts: 5

@Suresh let me correct your concept of overriding method in case of throw clause: An overriding method can throw All, None, Subset or subclass exceptions of the exceptions declared in throws class of overridden method in case of checked exceptions. Compiler validates this rule in case of checked exception only not in case of unchecked exceptions defined in throws clause.
Now consider your code you have defined unchecked exception in your overriding method's throws clause. so compiler will not gives any error in this case.
Note: java.lang.RuntimeException , java.lang.Error and their subclass comes under checked exceptions.

Hope it clears your doubt.
Do let me know if anything remains unclear.
Keep coding

I agree. Here's the link:
subject: overriding in java