aspose file tools*
The moose likes Associate Certification (OCAJP 7/8) and the fly likes Polymorphism + exeptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7/8)
Bookmark "Polymorphism + exeptions" Watch "Polymorphism + exeptions" New topic
Author

Polymorphism + exeptions

-Amit- Sharma
Greenhorn

Joined: Sep 04, 2012
Posts: 10
Question is we cannot use throws clause with overridden method definition if parent method does not have any. Why ?

class Parent
{
public void show()
{
}
}

class Child
{

public void show () throws IOException // this line will fail to compile
{
}

}

I was wondering how can we achieve this without touching base class's method, Since there is possibility that overridden method's implementation can throw checked exception and we don't want to handle in same method and want that to be thrown to calling method .

Second why this is not allowed?


Thanks .



Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

This is why it isn't allowed. Consider the following code (and this code assumes that Child extends Parent, which it doesn't in your code, but I think you meant it to).


Line 1 is fine. A Child IS-A Parent in this context, so the compiler will accept it.

Line 2 is fine. The compiler will only consider the reference type of the variable, and that is Parent. Parent.show() does not declare any checked exceptions.

But because we've allowed show() to be overridden to throw additional checked exceptions, there's a chance that this call could in fact throw an IOException. Which means we now have an unhandled checked exception - but the compiler is supposed to prevent this from happening. The problem is caused by the method being overridden in that way. So instead, the compiler doesn't allow it.

This is an example of the general principle (the "substitution principle") that instances of subclasses should be usable whenever an instance of the superclass is expected. Allowing overriding like that would break this principle.
-Amit- Sharma
Greenhorn

Joined: Sep 04, 2012
Posts: 10
Thanks Matthew Brown
Here is my understanding
1) To achieve polymorphism we cannot compromise with checked exception handling rule. since checked exceptions are supposed to handled properly and if not should not surpass the compilation.
2) substitution principle
Pranay Kurkure
Greenhorn

Joined: Sep 15, 2013
Posts: 1
Because IOException is check exception and in parent class's show() method we did not throws IOException or it's super class (Exception) hence we can not throws IOException or it's sub class in Child class's show() method

but we can throws uncheck exception

for e.g :-

class Parent
{
public void show()
{

}
}

class Child extends Parent
{
public void show() throes NullPointerException //This is allowed because NullPointerException is uncheck exception
{

}
}
Yin Stadfield
Ranch Hand

Joined: Oct 13, 2012
Posts: 43

Aside from unchecked/runtime exceptions, you can also throw Error or any of its subclasses on the overriden method! Though I'm not really sure why would anyone do that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism + exeptions