aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overiding method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overiding method " Watch "overiding method " New topic
Author

overiding method

josephine chen
Ranch Hand

Joined: Oct 29, 2002
Posts: 216

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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 22, 2008
Posts: 18944
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.
 
wood burning stoves
 
subject: overiding method