• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overiding method

 
josephine chen
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How come we don't get error when
the overiding method does not throw any exception
which the overidden method throws?
class base{void x() throws IOException{}}
class sub extends Base {void x() {}}

class sub extends Base {void x()throws Arithmetic Exception {}}
this compiles why???
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method which is overriden in the subclass can throw the same exception defined in the base class method or its subclasses or need not throw any exception.
I think in u'r case "Base" and "base" are two different classes Make sure u'r "sub" class extends "base" and not "Base"
I think it will work now.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The overiding method cannot throw any "CHECKED" exception other than which is thrown by overidden method (it can throw exceptions which derives ( can throw the same or subclasses of the exception thrown by the overidden method ).
Note that the overiding method need not not throw any exception BUT if it throws then it should comply the above rules.
--------------------------------------
"class sub extends Base {void x()throws Arithmetic Exception {}}
this compiles why???"
-------------------------
This compiles because "Arithmatic Exception" is a Runtime exception and the above rules do not apply here. the rules regarding overiding methods and exception is only for "CHECKED" exceptions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic