Interfaces are a type of class association. It promises to support the same behavior across multiple classes without the baggage of a particular implementation.
Essentially, you define an interface of abstract methods and properties and then add the interface to a class' definition and fulfill the interfaces' methods and properties. You can design classes in different descendant lines with common methods and properties without an interface but an interface ensures you implement the methods and properties using the same names and parameters.
- You do not create an object instance from an interface like you do with a class (you instantiate objects from classes that support the interface).
- Every class that wishes to support an interface implements all methods and properties specified by the interface.
If you are designing and programming a series of robots, you might create several interfaces so all your robot lines have similar command sets. You might design the following interfaces:
- ITalk - basic talking commands like speak.
- ITalkSmart - Inherits ITalk plus adds a database of responses to specific words and phrases.
- IMove - adds basic movement such as walk, arm movement, etc.
- ILife - adds basic life-like properties such as Birthday, DeathDay, Age, Weight, Height, etc.
- IHuman - adds human-like properties such as Sex, HairColor, EyeColor, etc.
Once you've designed your interfaces, you might create a series of robots that implement all or some variation of the interfaces above.
Interface-Based Polymorphism (Substitutability)
Substitutability allows a descendant class to be used anywhere an associated parent class is used. In object oriented programming a variable could refer to one object at one time and then another object another time. This allows the designer of software to create both a dog.run and a cat.run methods and then decide at runtime whether the variable will be a dog or a cat. Interfaces are a common coding element used to implement this type of polymorphism.
Interfaces allow for horizontal class design.
In a class tree, inheritance is used to design classes vertically with "is-a" relationships. You add functionality from top to bottom adding methods and properties to descendant classes. Interfaces allow you to design horizontally across your class tree using a "behaves-as" or "looks-like" relationship insisting certain classes implement a common interface.
Is support of interfaces a kind of multiple inheritance?
No, not in my opinion because interfaces are about specifying common interfaces but are missing implementation details. Some argue that interfaces are a form or type of multiple inheritance, but to me it is not. That would be like saying VB 6 Classic supported inheritance because it allowed you to establish a single abstract class and then implement it in several classes. A nice feature, but it just wasn't even close to a complete implementation of inheritance.