Given the code fragment:
interface SampleClosable {
public void close () throws java.io.IOException;
}
Which three implementations are 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 class extends an interface? .. This answer is wrong! (The answer comment doesn´t.. but the color green on the “Show answer” does)
Diego Parra, please give the right answer too when commenting anything please.
The right answer is A, a class implements an interface and the method might throw the same Exception or a lower one.
I don’t know why the author, seenagape is answering so wrong.
I know D and C are wrong. Get why A is right but is B also right
because IOException subclass of Exception.
im going with A & B
sully B is not right since IOException is the subclass not the supperClass of the Exception
An interface can extend another interface, similarly to the way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.
An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
Along with abstract methods an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.
Writing an interface is similar to writing a class. But a class describes the attributes and behaviours of an object. And an interface contains behaviours that a class implements.
Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.
“Which three implementations are valid?”
CORRECT ANSWER:
A. public class Test implements SampleCloseable {
public void close() throws java.io.IOException { / / do something } }
C. public class Test implements SampleCloseable {
public void close() throws java.io.FileNotFoundException { / / do something } }
E. public class Test implements SampleCloseable {
public void close() / / do something } }
a