- COMP.CS.140
- 6. Class Hierarchies
- 6.1 Modularity: Interfaces
- 6.1.2 Interface Classes
Interface Classes¶
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.
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:4o)