• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Inheritance and Exceptions

 
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys! I remember reading somewhere about the rules concerning inheritance or interface implementations and Exceptions that an overridden method must throw. Could you guys please brief me about the rules concerning this? Thank you very much! I'm reviewing for SCJP 1.5 BTW.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Tim,

An overriding method cannot throw a wider checked exception(s) thrown by overridden method (i.e., cannot throw parent exception of what is thrown in overridden method).

Overriding method is allowed to throw any unchecked exception(s).

Example.

import java.io.*;

class Tempo1
{


protected void method1() throws IOException
{
System.out.println("Im method1 in Tempo1 class");
}

}

public class Tempo extends Tempo1
{

public static void main(String[] args)
{


}

public void method1() throws Exception
{
System.out.println("Im method1 in Tempo class");

}



}


Thanks,

Raghav
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose I have a class with a method that might throw a certain type of exception...

Since I know meth() might throw MyException, I can write code to handle it...

So far, so good. But now suppose I extend MyClass and override meth(). What's okay for the overridden method to throw?

To answer this, consider what happens when an instance of the derived class is upcast to the base type, and used as a parameter in someOtherMethod...

someOtherMethod is coded to handle instances of MyException. So if the overridden meth() throws MyException or any subclass of MyException, then everything will still work as expected. But anything else it throws will not be caught. And if that "anything else" is a checked exception, then the code will not compile.

On the other hand, the overridden might not throw anything at all. And if that's the case, then it does not need to declare anything.
 
Hey! Wanna see my flashlight? It looks like this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic