Given the code fragment:
interface SampleClosable {
public void close () throws java.io.IOException;
}
Which implementation is valid?
A.
public class Test implements SampleCloseable {
Public void close () throws java.io.IOException {
// do something
}
}
B.
public class Test implements SampleCloseable {
Public void close () throws Exception {
// do something
}
}
C.
public class Test implementations SampleCloseable {
Public void close () throws Exception {
// do something
}
}
D.
public class Test extends SampleCloseable {
Public void close () throws java.IO.IOException {
// do something
}
}
Explanation:
To declare a class that implements an interface, you include an implements clause in the class declaration.
One interface might extended another interface, but a class cannot extend an interface. Checked exceptions are subject to the Catch or Specify Requirement. All exceptions are checked exceptions, except for those indicated by Error, RuntimeException, and their subclasses.
A
A & B
The Answer is A. To implement an interface, a class uses the implements keyword, not implementations or extends. To implement (override) a method from an interface, the implementing class’s method must have the exact same signature, including the exception that is thrown. Only Answer A meets these requirements.
The exception that is thrown in the implemented method can also be a subclass of the exception as defined in the interface (e.g., java.io.FileNotFoundException in the above example) or the implementing class can choose not to throw any exception.
The public keyword s written with capital “P”: the code won’t compile.