Repaso
EspaƱol
Java CoreFoundationsJV-52

What is a Java interface?

Options

  1. A contract: method signatures that a class promises to implement (Correct option)
  2. A visual screen that users see in the application
  3. A database connection that a class opens and closes
  4. A kind of loop that repeats until a condition is false

Answer

A contract: a group of method signatures that a class promises to implement; the compiler checks that it does

Why

The Java tutorial: interfaces form a contract between the class and the outside world, enforced at build time by the compiler.

If a class says it implements an interface, all its methods must exist or the code will not compile.

Example: interface Payment { void pay(int amount); } can be implemented by CardPayment and PayPalPayment. Callers only need to know the interface.

Remember

Interface = a contract that classes implement.

Key terms

Practice this question in Repaso