Which implementation is valid?

Given the code fragment:

interface SampleClosable {
public void close () throws java.io.IOException;
}

Which implementation is valid?

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.



Leave a Reply 5

Your email address will not be published. Required fields are marked *


Abed Abu Alhalawa

Abed Abu Alhalawa

A

Fredy Jimenez Rendon

Fredy Jimenez Rendon

A & B

James

James

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.

James

James

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.

Naira

Naira

The public keyword s written with capital “P”: the code won’t compile.