Interface
What is an Interface
- Background
- Interfaces were introduced to address limitations of multiple inheritance in Object-Oriented Programming
- They provide a way to achieve Polymorphism and define contracts for classes (What is the definition of “interface” in object oriented programming, Stack Overflow, n.d.)
- Definition:
- An interface is a programming structure that defines a contract specifying methods a class must implement
- It describes actions an object can perform without specifying how those actions are implemented (OOP - Interfaces, Germain, n.d.)
- An interface defines a set of abstract methods, properties, and events that implementing classes must provide (Interfaces - define behavior for multiple types - C#, Microsoft, n.d.)
- Practical uses:
- Interfaces allow for abstraction and separation of interface from implementation (What is an interface and why are they useful?, MyTutor, n.d.)
- They enable polymorphism by allowing different classes to be treated uniformly (PHP OOP Interfaces, W3Schools, n.d.)
- Interfaces facilitate code reuse and make it easier to change implementations without breaking existing code (What is an interface and why are they useful?, MyTutor, n.d.)
- Common Misconceptions
- Interfaces are not classes and cannot contain implementation code (except for default methods in some languages) (OOP - Interfaces, Germain, n.d.)
- Interfaces are not limited to just method declarations - some languages allow constants and static methods (What is the definition of “interface” in object oriented programming, Stack Overflow, n.d.)
Examples
Java example
- 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
- Defines common methods like start_engine() that all vehicle classes must implement (OOP - Interfaces, Germain, n.d.)
- Allows different vehicle types (car, truck, etc.) to be treated uniformly as “Vehicles” (OOP - Interfaces, Germain, n.d.)
Animal Sound Interface
- Defines a makeSound() method implemented differently by various animal classes (PHP OOP Interfaces, W3Schools, n.d.)
- Enables polymorphic behavior when working with different animal types (PHP OOP Interfaces, W3Schools, n.d.)
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
- Interfaces - define behavior for multiple types - C#
- Key points:
- Interfaces define functionality for multiple types
- They can include static and default implementations in some cases
- Interfaces enable multiple inheritance-like behavior for classes
Related Concepts
- Abstract ClassObjectOrientedProgrammingAbstraction
- Similar to interfaces but can include implementation and state
- PolymorphismObjectOrientedProgrammingInheritance
- Interfaces enable polymorphic behavior across different classes
- Multiple InheritanceObjectOrientedProgrammingInheritance
- Interfaces provide an alternative to multiple inheritance in some languages