• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Overriding - returns and exceptions

 
Ranch Hand
Posts: 41
Android Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it fair to say exceptions and return values have similar rules while overriding methods in subclasses?
(excluding runtime exceptions and freedom not to return exception)

They can return either the class type or the subclass type as return values/exception type.
Cannot return super class type as return value / exception type.
Cannot return new exception / return type.

There is much similarity here.

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sanjeev Ba wrote:Is it fair to say exceptions and return values have similar rules while overriding methods in subclasses?
(excluding runtime exceptions and freedom not to return exception)

They can return either the class type or the subclass type as return values/exception type.
Cannot return super class type as return value / exception type.
Cannot return new exception / return type.

There is much similarity here.



yes there are similarities. the difference can be that you can throw NO checked exceptions in your overridding method.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cannot return super class type as return value / exception type.


The above statement is correct. It can only return declared or sub-class of the declared checked exception.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the similarity over here is co-variance which is similar for object return types or exceptions thrown which would be object of the Exception Classes.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right that it's similar. In fact, the rules are as they are for exactly much the same reason. In each case, they're the only way the compiler can enforce the rules correctly. Consider this bit of code:
So, what are the restrictions on XXX and YYY? And why? Now consider this:
As far as the compiler is concerned, obj is a Superclass. So the method returns a SomeType, and throws a SomeException. So that's fine - that will compile OK. But at runtime, obj is a Subclass object. Which means:

- We're assigning an XXX value to a SomeType value.
- We might be throwing a YYY exception. If this is checked, it must be handled by the try/catch block.

The only way we can satisfy these is if XXX is SomeType or a subclass. And YYY can't include any checked exceptions that aren't SomeException or a subclass.

These rules are both related to the Liskov Substitution Principle: an instance of a class must always be replaceable by an instance of a subclass.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic