- IS-A vs. CAN-DO relationship: A type can inherit only one implementation. If the derived
type can’t claim an IS-A relationship with the base type, don’t use a base type; use an interface.
Interfaces imply a CAN-DO relationship. If the CAN-DO functionality appears to belong with
various object types, use an interface. For example, a type can convert instances of itself to
another type (IConvertible), a type can serialize an instance of itself (ISerializable), etc.
Note that value types must be derived from System.ValueType, and therefore, they cannot
be derived from an arbitrary base class. In this case, you must use a CAN-DO relationship and
define an interface. - Interface "is" scaled-down multi-inheritance,
- A class is always derived from one and only one class
- One of the great features of class inheritance is that it allows instances of a derived type to be
substituted in all contexts that expect instances of a base type. Similarly, interface inheritance allows
instances of a type that implements the interface to be substituted in all contexts that expect instances
of the named interface type. We will now look at how to define interfaces to make our discussion more
concrete.
Subscribe to:
Post Comments
(
Atom
)
0 comments:
Post a Comment