Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Polymorphic Exception behaviour

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ranchers,
Can anyone explain exactly why the following occurs:
//=================================
class Base{
void amethod()throws Exception{}
public static void main(String[] args){
//Base b = new Sub(); //1
Sub s = new Sub(); //2
s.amethod();
}
}
class Sub extends Base{
void amethod(){}
}
//==================================
The sample code is a subclass overriding the supers method. The Super method throws a checked exception. I am aware of the rules that any subclass overriding methods may throw the same or less but not more checked exceptions than the super method. However in the above example - if I compile as is - it compiles fine. No exception needs to handled or rethrown.
When I uncomment line 1, I get the normal unreported exception compiler error.
If I place a try/catch block with 1 commented the compiler complains no exception is thrown.
My question to all you gurus out there is why do the above occur? Why isnt there consistency?
What is the precise reason for this behaviour?
I have read through the Language Specs but couldn't find anything.
Please help!!
Regards
Stephen Batsas
SCPJ2
 
Stephen Batsas
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Ranchers,
I left out a line in the main:
//b.method(); //3
This is uncommented when line 1 is uncommented.
And vice verse.
Regards
Stephen Batsas
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer lies within your subject - Polymorphic behaviour.
For example, when you are expecting a base object from a module, what you might finally end up getting could be an object of instance base, sub or any of the base's derived classes. Since we are assuming that we are dealing with the base class object we can only handle the exceptions based on the desired method's signature in the base class. That is the reason why sub classes cannot throw more exceptions or exceptions that are not derived from the ones that are mentioned in the parent's method signature.
But if you are directly dealing with the sub class, you don't have to worry about the same method in the super class because an object of the sub class would always use it's own method instead of the base class one.
HTH
 
You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad.
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic