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

Method overriding

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was giving a mock exam http://www.vivek.4mg.com/javacertification/jsexam.htm
and i run into this question

Which of the following are legal method declarations to add to the class Sub? Assume that each method is the only one being added.

a) public static void main( String args[] ){}
b) float methodTwo(){}
c) long methodOne( int c, long d ){}
d) int methodOne( int c, long d ) throws ArithmeticException{}
e) int methodOne( int c, long d ) throws FileNotFoundException{}


I answered a,b,d,e

but the answer was a,b,e.

What's wrong with d.I think Arthmeticexception is unchecked exception and a runtime exception.

Please clear my doubt.
[ October 07, 2005: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is compiling with option d also.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

a,b,d,e compiles fine and doesn't violate anything.

It's an error for the MindQ's Exam.

But the most important thing that you need to do here is try each code if you're having doubts so that this will clear everything.
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
d will not compile because it tries to override the same method in the super class and adds more exceptions.
It would have worked if ArithmeticException would have been a subclass of IOExcpetion, but that is NOT the case, as you can see

java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.ArithmeticException

And the rule in java is, an overriding method cannot throw newer exceptions, other than subclasses of those in the overridden method or the same exceptions.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the rule in java is, an overriding method cannot throw newer exceptions, other than subclasses of those in the overridden method or the same exceptions.

That's true but it is valid for only checked exceptions. ArithmeticException
is unchecked exception. Pl. provide your compiled code if you believe that
'd' is resulting in compile time error.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rule for overriding methods and Exceptions is:

The overriding method:

-Must not throw new or broader checked exceptions

-May throw fewer or narrower checked exceptions or any unchecked exceptions.

So if the overriding method is throwing an unchecked exception and the rest of the method definition is in line with the method being overridden then it should be fine.

Fes
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I take my words back, just reexamined the JLS and what you say is true,

I was misled by the tutorial on the sun site


The overriding method can have a different throws clause as long as it doesn't specify any types not specified by the throws clause in the overridden method.


from http://java.sun.com/docs/books/tutorial/java/javaOO/override.html

Extremely sorry about that.

So, I quote the correct one from the JLS


More precisely, suppose that B is a class or interface, and A is a superclass or superinterface of B, and a method declaration n in B overrides or hides a method declaration m in A. If n has a throws clause that mentions any checked exception types, then m must have a throws clause, and for every checked exception type listed in the throws clause of n, that same exception class or one of its supertypes must occur in the erasure of the throws clause of m; otherwise, a compile-time error occurs.

reply
    Bookmark Topic Watch Topic
  • New Topic