Java FAQ: Top Questions
13. What are interfaces in Java?
An interface in Java is a blueprint of a class, defining method signatures (and constants) that implementing classes must provide. It supports abstraction and multiple inheritance.
-
Declaration:
Defined with the
interfacekeyword. -
Implementation:
Classes implement interfaces using the
implementskeyword. -
Methods:
Before Java 8, interfaces had only abstract methods (no body). Since Java 8, they can include
defaultandstaticmethods with implementations. - Multiple Inheritance: A class can implement multiple interfaces, unlike extending multiple classes.
-
Use Case:
Defines contracts for classes (e.g.,
Comparable,Serializable).
