exceptions and extending classes

Hello.

I just came across some code that I expected would not compile, but it does. I have an interface that defines a method such as:

public String getString() throws Exception;

When i implement this in a class, it seems that i can omit the throws exception from my impleementation's method signature, such as:

public String getString() {

return "bob";

}

I fully understand that you can't throw an exception if there's no chance to create one, as in my simple method logic. But, if you don't have to have the throws statement in your implementation's signature, what does its presence in the interface imply?

Message was edited by:

chadmichael

[711 byte] By [chadmichaela] at [2007-11-27 6:19:15]
# 1

Hi chadmichael,

The "throws" clause is an optional modifier and doesn't belong to the methods signature (you can change modifiers when overrighting a method). More information here:

http://java.about.com/library/weekly/aa_methods1.htm

http://www.jguru.com/faq/view.jsp?EID=15776

Greets,

A. Pinto

apintoa at 2007-7-12 17:33:41 > top of Java-index,Java Essentials,Java Programming...
# 2
You can throw less exceptions in subclasses or implementing classes, you just can't throw more (checked exceptions) than is specified in the superclass.
-Kayaman-a at 2007-7-12 17:33:41 > top of Java-index,Java Essentials,Java Programming...