Extending an abstract base class
suggest changeUnlike interfaces, which can be described as contracts for implementation, abstract classes act as contracts for extension.
An abstract class cannot be instantiated, it must be extended and the resulting class (or derived class) can then be instantiated.
Abstract classes are used to share an implementation with every derived class.
public abstract class Car
{
public void HonkHorn() {
// Implementation of horn being honked
}
}
public class Mustang : Car
{
// Simply by extending the abstract class Car, the Mustang can HonkHorn()
// Before default interface methods, an interface could not supply this body,
// so every class that implemented it would have written HonkHorn.
}
The above example shows how any class extending Car will automatically receive the HonkHorn method with the implementation. A developer who creates a new Car does not have to write the horn.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents