Extending an abstract base class C# with Example



Extending an abstract base class C# with Example

Unlike 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 provide generic implementations 
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() 
// If Car were an interface, the HonkHorn method would need to be included 
// in every class that implemented it. 
} 
The above example shows how any class extending Car will automatically receive the HonkHorn method with the 
implementation. This means that any developer creating a new Car will not need to worry about how it will honk it's 
horn. 

0 Comment's

Comment Form