This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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.
 
jQuery in Action, 2nd edition
 
subject: overiding method
 
Similar Threads
Overiding and Exceptions
Overiding with Exceptions
Regarding Overiding
overiding method(own code)
Overiding