About Interfaces
Polymorphism is available not only via inheritance, but also via interfaces. Unlike abstract classes, interfaces cannot include any implementation.
Like abstract classes, however, interfaces define a set of members that classes can rely on in order to support a particular feature.
By implementing an interface, a class defines its capabilities. The interface implementation relationship is a "can do" relationship: The class can do what the interface requires. The interface defines the contract between the classes that implement the interface and the classes that use the interface. Classes that implement interfaces define methods with the same signatures as the implemented interfaces.
Consider the following example: An innumerable number of file compression formats are available (.zip, .7-zip, .cab, .lha, .tar, .tar.gz, .tar.bz2, .bh, .rar, .arj, .arc, .ace, .zoo, .gz, .bzip2, .xxe, .mime, .uue, and .yenc, just to name a few). If you created classes for each compression format, you could end up with different method signatures for each compression implementation and no ability for a standard calling convention across them. Although the method signature could be defined in an abstract member of a base class, deriving from a common base type uses up a class’s one and only inheritance, with an unlikely chance of sharing code across the various compression implementations, thereby making the potential of a base class implementation useless.
Instead of sharing a common base class, each compression class needs to implement a common interface. Interfaces define the contract a class supports to interact with the other classes that expect the interface. Although there are many potential compression algorithms, if all of them could implement the
In addition, when you derive from an abstract class, you must override all the abstract methods in the abstract base class, but you don’t have to override any nonabstract methods. You can simply use the implementation that the base class provides. This is called partial implementation, and it’s very common with abstract classes. Interfaces don’t have any implementation, so you must implement every method defined in the interface. You can’t partially implement an interface.
Inheriting from an abstract class implements the is-a relationship. Implementing an interface defines a the implements relationship. These two relationships are subtly different. A car is a vehicle, but it might implement the
By implementing an interface, a class defines its capabilities. The interface implementation relationship is a "can do" relationship: The class can do what the interface requires. The interface defines the contract between the classes that implement the interface and the classes that use the interface. Classes that implement interfaces define methods with the same signatures as the implemented interfaces.
Why Interfaces?
Implemented interfaces are like appliances with wall plugs. The wall plug is the interface that appliances support in order to receive AC power. An appliance can use that power in countless ways, but in order to plug into a wall socket, an appliance must supply a compatible wall plug. What the appliance does with the power corresponds to how an interface implementation varies from class to class. The specification that defines a wall plug is the contract that must be supported for an appliance to plug into the wall plug. Similarly, an interface defines a contract that a class must support to gain the capability that the interface provides.Consider the following example: An innumerable number of file compression formats are available (.zip, .7-zip, .cab, .lha, .tar, .tar.gz, .tar.bz2, .bh, .rar, .arj, .arc, .ace, .zoo, .gz, .bzip2, .xxe, .mime, .uue, and .yenc, just to name a few). If you created classes for each compression format, you could end up with different method signatures for each compression implementation and no ability for a standard calling convention across them. Although the method signature could be defined in an abstract member of a base class, deriving from a common base type uses up a class’s one and only inheritance, with an unlikely chance of sharing code across the various compression implementations, thereby making the potential of a base class implementation useless.
Instead of sharing a common base class, each compression class needs to implement a common interface. Interfaces define the contract a class supports to interact with the other classes that expect the interface. Although there are many potential compression algorithms, if all of them could implement the
IFileCompression
interface and its Compress
and Uncompress
methods, the code for calling the algorithm on any particular compression class would simply
involve a cast to the IFileCompression
interface and a call into the members, regardless of which class implemented the methods. The result is
polymorphism because each compression class has the same method signature but individual implementations of that signature.
Interfaces Versus Abstract Base Classes
It's often confusing to identify difference between an interface and an abstract base class. The key difference is that an abstract base class serves as the base class for a family of derived classes, and an interface is meant to be mixed in with other inheritance chains. That is, a class can inherit from only a single parent class, but it can implement multiple interfaces.In addition, when you derive from an abstract class, you must override all the abstract methods in the abstract base class, but you don’t have to override any nonabstract methods. You can simply use the implementation that the base class provides. This is called partial implementation, and it’s very common with abstract classes. Interfaces don’t have any implementation, so you must implement every method defined in the interface. You can’t partially implement an interface.
Inheriting from an abstract class implements the is-a relationship. Implementing an interface defines a the implements relationship. These two relationships are subtly different. A car is a vehicle, but it might implement the
CanBeBoughtWithLease
capability (as can a house or appartment).
0 comments:
Post a Comment