What is a Java interface?
Options
- A contract: method signatures that a class promises to implement (Correct option)
- A visual screen that users see in the application
- A database connection that a class opens and closes
- 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.