Monday, 3 October 2011

Interfaces, Abstract Classes, Abstract Methods and Virtual Methods

Here is a synopsis about Abstract Classes/Methods:

  1. Only abstract classes can have abstract members.
  2. A non-abstract class that inherits from an abstract class must override its abstract members.
  3. An abstract member is implicitly virtual.
  4. An abstract member cannot provide any implementation (abstract is called pure virtual in some languages).
  5. An abstract class cannot be instantiated
I was reading some old notes of mine and got to thinking: can an abstract class have virtual methods?

The answer is 'implicitly, yes, explicitly, no' - confusing answer, yes? Point 3 above explains it. Any 'abstract' method is, in fact, 'virtual' in that it can be implemented by a deriving class - and in fact, if it is declared 'abstract', it MUST be implemented by a deriving class.

In the case of an Interface:

  1. No Interface members can provide any implementation
  2. An implementing class (note that Interfaces are 'implemented', not 'inherited' from) must implement all members of the Interface

No comments:

Post a Comment