In Focus

How To Use Abstract Class, Abstract Method, And Abstract Property In C#

In this article, we look at what an abstract class is, its method and properties, and how to use them.

In this article, we look at what an abstract class is. We also look at abstract methods, abstract properties and non-abstract members of an abstract class. We also look at how to implement an abstract class in C#.
 
The abstract modifier indicates that a class or its members are abstract and all derived classes from an abstract class must implement these members. An abstract class can't be initialized. Use of an abstract modifier in a class declaration indicates that a class is intended only to be a base class of other classes. The members of the class don't have an implementation in a class with an abstract modifier. The abstract modifier can be used with classes, methods, properties, indexers, and events. Members marked as abstract or included in an abstract class must be implemented by classes that derive from the abstract class.
 

Abstract Class

  • An abstract class cannot be instantiated.
  • An abstract class not only contains abstract methods and assessors but also contains non-abstract methods, properties, and indexers.
  • It is not possible to modify an abstract class with the sealed modifier because the two modifiers have opposite meanings. The sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.
  • When a class contains at least one abstract method, then the class must be declared as an abstract class.
  • When a class is declared as an abstract class, then it is not possible to create an instance for that class. It can, however, be used as a parameter in a method.
  • An abstract class can't be a static class.
  • An abstract class is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards. 

Abstract Method/Properties

  • An abstract method/property has a signature in an abstract class.
  • An abstract method/property has an implementation in a derived class.
  • An abstract method/property can't be static.
  • An abstract method/property is implicitly a virtual method/property.
  • An abstract method/property is implemented in a derived class using the override keyword. 

Interface versus Abstract Class

  • We can't define fields in an interface but can define fields in an abstract class.
  • The interface has a signature of methods but an abstract class can contain both types of methods; these have a signature or an implementation.
  • Interface members are by default public and can't use an access specifier for them but in an abstract class, we can define an access specifier for each member.
  • An interface is slow because it needs to find the actual method in the corresponding classes. But an abstract class is fast.
  • We can inherit from multiple interfaces for a single class but can't inherit multiple abstract classes. 

Example of Abstract Class

 
Let’s look at how abstract classes are implemented. Let’s assume the Vehicle is an abstract class that is the base class for the Car class.
 
The Vehicle class has abstract members that must be implemented by the Car class or any other class that is inherited from the Vehicle class.
 
The Vehicle class has three abstract members, two properties - Distance and Time, and a method - Speed. 
  1. using System;  
  2. namespace AbstractExample  
  3. {  
  4.     abstract class Vehicle  
  5.     {  
  6.         public abstract double Distance { getset; }  
  7.         public abstract double Time { getset; }  
  8.         public abstract double Speed();  
  9.     }   
  10.     class Car : Vehicle  
  11.     {  
  12.         double mDistance, mTime = 0.0;  
  13.         public override double Distance  
  14.         {  
  15.             get  
  16.             {  
  17.                 return mDistance;  
  18.             }  
  19.             set  
  20.             {  
  21.                 if (value <= 0)  
  22.                 {  
  23.                     mDistance = 1;  
  24.                 }  
  25.                 else  
  26.                 {  
  27.                     mDistance = value;  
  28.                 }  
  29.             }  
  30.         }  
  31.         public override double Time  
  32.         {  
  33.             get  
  34.             {  
  35.                 return mTime;  
  36.             }  
  37.             set  
  38.             {  
  39.                 if (value <= 0)  
  40.                 {  
  41.                     mTime = 1;  
  42.                 }  
  43.                 else  
  44.                 {  
  45.                     mTime = value;  
  46.                 }  
  47.             }  
  48.         }   
  49.         public override double Speed()  
  50.         {  
  51.             double speed = 0.0;  
  52.             double hours = mTime / 60;  
  53.             speed = mDistance / hours;  
  54.             return speed;  
  55.         }  
  56.     }   
  57.     class Program  
  58.     {  
  59.         static void Main(string[] args)  
  60.         {  
  61.             double speed = 0.0;  
  62.             Car objCar = new Car();  
  63.             Console.WriteLine("Enter the Distance");  
  64.             objCar.Distance = Double.Parse(Console.ReadLine());  
  65.             Console.WriteLine("Enter the time in minutes");  
  66.             objCar.Time = Double.Parse(Console.ReadLine());  
  67.             speed = objCar.Speed();  
  68.             Console.WriteLine("Car speed is {0:0.00}", speed);  
  69.             Console.Read();  
  70.         }  
  71.     }  
  72. }  
In the above code, the Car class is derived from the Vehicle class and implements all three abstract members. 
 
The Program class uses the Car class, creates an instance of it, sets its properties, and calls its methods.  
 
The output looks like the following.
 
How To Use Abstract Class, Abstract Method, and Abstract Property In C#

Summary

 
In this article and code sample, we saw how to use and implement abstract classes in C#.