Question

I'm a beginner studying interfaces in Java through some quizzes and I came through this question:

What are Java Interface used for?

I can opt among one of the following three choices:

A. They're used to describe the API of various classes.

B. They're used to avoid having to specify the contract for methods.

C. They're used to let real and apparent types differ. You can obtain this difference only by using interfaces.

I think the right answer is Choice A. Choice B doesn't make sense to me, since Interfaces usually describe methods along with their contract. Choice C doesn't make sense either, but I might be wrong.

Which one is the right answer? Thank you!

Was it helpful?

Solution

A. They're used to describe the API of various classes.

Correct.

I struggle with the correctness of the phrasing. It's not wrong, but it is essentially a confusing tautology. But they had to avoid calling it "the interface" to not give away the answer, and I'm struggling to find a better (and similarly terse) alternative.

B. They're used to avoid having to specify the contract for methods.

Quite the opposite, the interface is the contract.

C. They're used to let real and apparent types differ. You can obtain this difference only by using interfaces.

The first part of the statement is correct, and is basically describing polymorphism.

The second part of the statement is incorrect, since polymorphism also applies to inheritance.

Licensed under: CC-BY-SA with attribution
scroll top