• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Exceptions and abstract method declarations

 
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
Please could someone explain to me:

1) Why throws Exception is not allowed if supplemented below?
2) Can we say that we are overriding the method declaration in the Subclass?





The source of this question comes from the Online Udemy Course "Pass the Oracle Certified Associate(OCA): Java SE 8 Programmer I EXAM"
Instructor: Udayan Khattry
 
Clemonte Johnstone
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, is anyone able to help please in relation to the above?
 
Enthuware Software Support
Posts: 4525
45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clemonte Johnstone wrote:Hi, is anyone able to help please in relation to the above?


There is a fair bit of theoretical reasoning behind it, which is better explained in a book or a tutorial. Which book are you following?
 
Master Rancher
Posts: 3827
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clemonte Johnstone wrote:1) Why throws Exception is not allowed if supplemented below?


Because when you override a method, the new method may not add any new checked exception types.  

In your example, throwing ArrayIndexOutOfBoundsException is OK, because (a) it's unchecked, and (b) it's a subtype of IndexOutOfBoundsException.  You're not throwing a new checked exception; you're saying you won't throw any IndexOutOfBoundsException other than the more specific subtype ArrayIndexOutOfBoundsException.  That's allowed, even if these were checked exceptions (which they are not).

But throws Exception is definitely a new type of checked exception.  It includes the unchecked IndexOutOfBoundsException and ArrayIndexOutOfBoundsException, but also may include many other things like IOException.  It could be anything.  Definitely not allowed by the super method declaration.

Clemonte Johnstone wrote:2) Can we say that we are overriding the method declaration in the Subclass?


Yes.  You are overriding it, providing a method with the same signature.  You are not implementing it, since it's still abstract, no implementation, but you are overriding it.  This can be done to modify the method in a few allowed ways, including (a) making the method more accessible (not less), (b) changing the return type to a more specific subtype, if applicable, (c) changing the throws clause to a more restrictive (not less) list of checked types (plus any unchecked types you like), and (d) adding a new JavaDoc comment (which hopefully modifies but does not violate the contract of the super method -- that's up to you though, the compiler doesn't care)
 
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic