• Post Reply Bookmark Topic Watch Topic
  • New Topic

what are the rules for how inheritence and exception work together?  RSS feed

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help me with how inheritence and exception work together ?? what are the rules ??
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Not sure I understand the question. Do you mean what are the rules about throwing Exceptions from overriding methods?
What do you know about the Liskov Substitution Principle (LSP)? It says that all methods of subtypes must be used as if they were the same as supertype methods without the caller noticing the difference. That means that in all instances where the supertype method is feasible, that the subtype method must be feasible, too. That means that in all instances where the supertype method doesn't throw an Exception, the subtype method doesn't throw an Exception either.
If you throw the same Exception under the same circumstances, that is all right.
If you throw a subtype of the same Exception in a subset of those circumstances, that is all right too.

The javac tool can only verify checked Exceptions. That means it will fail to compile code where an overriding method declares a checked Exception unless that Exception is a subtype of the exceptions declared by the supertype method.
Remember that any type can be a subtype of itself, so the javac tool permits a method to declare the same Exception as its overridden version.
 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing to look out for, is that if you redeclare or define a method that was inherited from two different types, the exceptions it can throw are the intersection of the exceptions the super-types can throw, not the union:
 
Akshay Rawal
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akshay Rawal wrote:please help me with how inheritence and exception work together ?? what are the rules ??
.
.hi..to be more specific...i am unable to get following code....although it is well explained..please if we can break this code and explain with more simple examples..it will be much helpful for me...

 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you call a method that declares that it throws a checked exception you must either wrap that call in a try/catch or declare that your method in turn throws the checked exception.
Let's say that calling a method that declares checked exceptions is more expensive than calling a method that doesn't throw any exceptions (because of the extra work that we have to do).

Now, the main rule with inheritance is that if you extend a class, your new class must not be more expensive to use than the class you are extending*. It's fine to make it less expensive but not more expensive.
So when you override a method you are allowed to specify that the method doesn't throw any exceptions even if the original method threw some (thus making it less expensive) but you are not allowed to add new exceptions to the method which would make it more expensive to call the new method.








(*)If there is code that was using objects of the old class you are extending then if you replace those with objects from your new child class the code should still work without any modification. This is called the Liskov substitution principle.
 
Akshay Rawal
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much. i got your explanation ..very well explained..








 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good grief! What illegible code. Please space the lines out, indent them correctly, and always extend Exception by classes called Exception. Not class Foul extends FootballException, but FoulException, please.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!