2

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!

  • 3
    The first answer is heading into tautology territory, but the other two are factually incorrect. I'm not sure you'll learn anything from such a quiz – Caleth Jan 15 at 14:39
  • @Caleth You don't learn from quizzes. You check your understanding. One way to do this is to write a tautology (or rather, the same thing in different terms) and see if the reader can tell this. This is obviously a quiz for beginners. – Andres F. Jan 15 at 19:02
2

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.

  • 2
    That's the problem with a Java-esque interface being a specific severely restricted and anemic attempt at capturing the distorted reflection of what an interface is. – Deduplicator Jan 15 at 15:02
  • polymorphism also applies to classes at the end surely? – jk. Jan 15 at 16:01
  • @jk. Not sure what you mean with "classes at the end". It applies to relations (i.e. interface + its implementation, or base class + derived class) rather than to a single class. – Flater Jan 15 at 20:40
  • I meant that class inheritance is the specific example of polymorphism happening not through interfaces – jk. Jan 17 at 9:58
  • @jk.: Unless I'm misunderstanding your, that is exactly what the last sentence of my answer conveys. – Flater Jan 17 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.