Interfaces
An interface defines a contract. It declares which public methods a class must implement, but it does not provide any implementation.
Interfaces are declared with the interface keyword. All methods inside an interface are implicitly abstract and must be public.
When a class uses implements, it agrees to fulfill the contract. It must define every method declared in the interface.
Unlike abstract classes:
- Interfaces cannot have properties.
- Interfaces cannot contain implemented methods.
- A class can implement multiple interfaces.
- A class can extend a parent class and implement an interface at the same time.
Conceptually:
Abstract class → “is a” relationship with shared structure.
Interface → “can do” capability contract.
Interfaces enable polymorphism across unrelated classes. If multiple classes implement the same interface, they can be treated uniformly based on shared behavior.