Interface

What is an Interface

Examples

Java example

public interface Insurable {
   double getPremium();
}
 
public interface InterestBearing {
   double getInterestRate();
}
  • All methods in an interface are implicitly public and abstract
    • They don’t have method implementations, so there’s just a semicolon
  • Java does not support multiple inheritance of classes, but it supports multiple inheritance of interfaces
  • Interfaces cannot have instance fields
    • Constants can be defined in interfaces, and they are implicitly public static final

Vehicle Interface

Animal Sound Interface

Literature Review

Germain, n.d

  • OOP - Interfaces
  • Key points:
    • Interfaces enforce properties on objects
    • They describe actions without implementation details
    • Interfaces enable treating different classes uniformly

Microsoft, n.d