Abstract classes
Beside the implementation of interfaces there is another way to describe what subclasses have to do, without necessarily to prescribe how this is to be done: the so-called extension of abstract classes.
When a superclass is much to abstract to be instantiated meaningfully, it should be forbidden to create objects of this class. This can be done by declaration of abstract classes. Only subclasses that extend these abstract classes and are not themselves beeing abstract can be instantiated.

UML class diagram: extension of an abstract class
Class Account is abstract and cannot be instantiated:
- abstract class Account// abstract class because at least one method is declared as abstract
- {
- protected double balance; // attributes are possible
- public Account(double initialCredit) // constructors are possible
- {
- balance = initialCredit;
- }
- public double getBalance() // concrete method, will be inherited
- {
- return balance;
- }
- public abstract int transaction(double amount); // abstract method, has to be implemented by subclasses
- }
The abstract class Account is implemented by two classes:
- class SavingsAccount : Account// implementation of an abstract class
- {
- private double interestRate;
- public SavingsAccount(double initialCredit, double interestRate)
- : base(initialCredit) // call of superclass constructor
- {
- this.interestRate = interestRate; // additional code
- }
- public override int transaction(double amount) // method has to be implemented,
- // because was declared abstract in superclass
- { // by the use of keyword 'override'
- if (balance + amount < 0)
- return -1;
- else
- {
- balance += amount;
- return 0;
- }
- }
- }
- class CheckingAccount : Account
- {
- public CheckingAccount(double initialCredit)
- : base(initialCredit) // call of superclass constructor
- {
- }
- public override int transaction(double amount) // method has to be implemented, because was declared abstract
- { // in superclass
- balance += amount;
- return 0;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
-
Account account = new Account(0);// 'Account' is abstract, cannot be instantiated - SavingsAccount savingsAccount = new SavingsAccount(50, 2.5);
- CheckingAccount checkingAccount = new CheckingAccount(0);
- Console.WriteLine("Balance of savingsAccount: " + savingsAccount.getBalance()); // inherited methods
- Console.WriteLine("Balance of checkingAccount: " + checkingAccount.getBalance());
- Console.ReadKey();
- }
- }
So, why not simply implement interfaces in subclass? An interface is abstract too and can not be instantiated?
The answer is,
- an abstract class can define concrete methods, which don't have to be defined in subclass and
- an abstract class can have a state, but an interface cannot in C#. In other words an abstract class can have attributes for saving a state.
| Feature | Abstract Classes |
Interfaces |
|---|---|---|
| Instantiable | no | no |
| Type of methods |
abstract non-abstract static non-static |
abstract |
| Accessibility of methods | private protected public |
public |
| Fields (attributes) |
yes |
no |
| Accessibility of attributes |
private protected public |
public |
| Can implement/extend |
multiple interfaces one abstract class |
multiple interfaces |
| Multiple implementation |
no | yes |
| Constructor | yes | no |