Abstract Classes
Each type of Control
has a different shape and appearance. Drop-down ListBox
es look very different from Button
s. Clearly, every subclass of Control
should implement its own DrawControl( )
method—but so far, nothing in the Control
class enforces that they must do so. To require subclasses to implement a method of their base, you need to designate that method as abstract, rather than virtual.
An abstract method has no implementation. It creates a method name and signature that must be implemented in all derived classes. Furthermore, making at least one method of any class abstract has the side effect of making the entire class abstract.
Abstract classes establish a base for derived classes, but it is not legal to instantiate an object of an abstract class. Once you declare a method to be abstract, you prohibit the creation of any instances of that class.
Thus, if you were to designate DrawControl( )
as an abstract method in the Control
class, the Control
class itself would become abstract. Then you could derive from Control
, but you could not create any Control
instances. That makes sense, because the Control
class is an abstraction—there is no such thing as a simple Control
object, only objects derived from Control
.
Making Control.DrawControl( )
abstract means that each class derived from Control
would have to implement its own DrawControl( )
method. If the derived class failed to implement the abstract method, that derived class would also be abstract, and again no ...
Get Learning C# 3.0 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.