An interface acts as different roles in object oriented programming.
As a concept the purpose of the interface is to tell the user of the component how it is meant to be used and how it is meant to work.
Each class also has an interface that fullfills the role of the interface i.e. it tells how the objects of the class behave on the outside.
It is quite common in class hierachies that functionality is not implemented in abstract base classes: thay contain only a depiction of the interface functions.
Additionally it is common to face situations where some functionality does not directly fit any specific point in the inheritance tree.
Interface classes are useful in these situations. They are classes that act only as abstract base classes without any inner implementation.
Their role in the hierachy is only to reveal a public interface.
The implementation of the interface is done in concrete classes into whose public interface the methods defined by the interface class are needed.
Class hierarchy for organims using interface classes.¶
In the organism example moving and laying eggs were functionality that didn’t directly fit into the hierarchy.
Abowe the same class hierarchy has been implemented using interface classes Movable and Oviparous.
Now the common behaviour for the classes can be brought into their interface by using the itnerface class.
All animals move so on the base class level Animal implement moving with the help of the interface class.
This way moving can also be included into SlimeMold even though it is on the other side of the class hierarchy from the point of view of the rest of its behaviour.
With the help of interface classes several classes can implement the same interface without the relation to each other in the inheritance hierarchy.
On class can also implement several interfaces if need be.
In programming languages interface classes have been implemented with different mechanisms.
The most interesting for us is naturally Java where interface classes have an implementation separate from classes: Interface.
Interface Classes (kesto 8:40)
Which of the following hold for interface classes
When a class is inherited from an interface class, it has access to the implementations of the member functions of the interface class.
The role of interface classes in the hierachy is only to reveal a public interface.
Interface classes provide a common implementation of the interface to their subclasses.
When a class inherits from an interface class it gets the interface defined by the interface class, which it must implement itself, to its use.
Abstract base classes and interface classes are the same thing.
With the help of interface classes several classes on different sides of the class hierarchy can implement the same interface
The purpose of class hierarchies is to show which classes use services from which interface class.
Only one sub class can implement the interface defined by the interface class in the program.