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

UML class diagram: extension of an abstract class


Class Account is abstract and cannot be instantiated:

  1. abstract class Account// abstract class because at least one method is declared as abstract
  2. {
  3.     protected double balance;               // attributes are possible
  4.  
  5.     public Account(double initialCredit)           // constructors are possible
  6.     {
  7.         balance = initialCredit;
  8.     }
  9.     public double getBalance()              // concrete method, will be inherited
  10.     {
  11.         return balance;
  12.     }
  13.     public abstract int transaction(double amount);     // abstract method, has to be implemented by subclasses
  14. }


The abstract class Account is implemented by two classes:

  1. class SavingsAccount : Account// implementation of an abstract class
  2. {
  3.     private double interestRate;
  4.  
  5.     public SavingsAccount(double initialCredit, double interestRate)
  6.         : base(initialCredit)             // call of superclass constructor
  7.     {
  8.         this.interestRate = interestRate;       // additional code
  9.     }
  10.     public override int transaction(double amount)  // method has to be implemented,
  11.                                                     // because was declared abstract in superclass
  12.     {                                               // by the use of keyword 'override'
  13.         if (balance + amount < 0)
  14.             return -1;
  15.         else
  16.         {
  17.             balance += amount;
  18.             return 0;
  19.         }
  20.     }
  21. }
  22.  
  23. class CheckingAccount : Account
  24. {
  25.     public CheckingAccount(double initialCredit)
  26.         : base(initialCredit)                        // call of superclass constructor
  27.     {
  28.     }
  29.     public override int transaction(double amount)   // method has to be implemented, because was declared abstract
  30.     {                                                // in superclass
  31.         balance += amount;                           
  32.         return 0;
  33.     }
  34. }

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         Account account = new Account(0);       // 'Account' is abstract, cannot be instantiated
  6.  
  7.         SavingsAccount savingsAccount = new SavingsAccount(50, 2.5);
  8.         CheckingAccount checkingAccount = new CheckingAccount(0);
  9.  
  10.         Console.WriteLine("Balance of savingsAccount: " + savingsAccount.getBalance());        // inherited methods
  11.         Console.WriteLine("Balance of checkingAccount: " + checkingAccount.getBalance());
  12.  
  13.         Console.ReadKey();
  14.     }
  15. }


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.



Abstract Class vs. Interface 
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


Last modified: Friday, 6 September 2019, 9:18 AM