Please enable JavaScript.
Coggle requires JavaScript to display documents.
09 Abstract classes and interfaces (Characteristics of abstract classes…
09
Abstract classes
and interfaces
The need for
abstract classes
Virtual members can be overridden,
but don't have to be
What if want to force derived classes
to provide an implementation of member?
Characteristics of
abstract classes
Class is abstract if any member is abstract
Abstract classes cannot be instantiated directly
Sets contract for derived classes (must implement...)
Derived class inherits all concrete + abstract members
Concrete class (class that can be instantiated)
must provide implementations of all base class
abstract members
Abstract class is a type, so substitutability / casting
rules apply to it just like non-abstract classes
Derived class can be used anywhere abstract base class could have been used
Members are defined in the base abstract class
(via "abstract" modifier) but are not implemented there
Abstract members are implemented in derived class
via "override" modifier
Abstract class can contain mix of abstract and
non-abstract members
Interfaces
Like fully abstract class
No implementation at all
No fields, no statics
Should be a group of related members
Can be implemented by any class
If class implements an interface, reference is of that type
Interface is a type
Class can implement multiple interfaces
Classes that implement same interface
can be otherwise unrelated
Multiple
inheritance?
Strictly, no
But, base class can inherit
from another base class
Class can implement
multiple interfaces
Labs
Bird / penguin / duck / fish