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

Introduction

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.

What is 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. 

What is 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. 

What is 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. 

using System;  
namespace AbstractExample  
{  
    abstract class Vehicle  
    {  
        public abstract double Distance { get; set; }  
        public abstract double Time { get; set; }  
        public abstract double Speed();  
    }   
    class Car : Vehicle  
    {  
        double mDistance, mTime = 0.0;  
        public override double Distance  
        {  
            get  
            {  
                return mDistance;  
            }  
            set  
            {  
                if (value <= 0)  
                {  
                    mDistance = 1;  
                }  
                else  
                {  
                    mDistance = value;  
                }  
            }  
        }  
        public override double Time  
        {  
            get  
            {  
                return mTime;  
            }  
            set  
            {  
                if (value <= 0)  
                {  
                    mTime = 1;  
                }  
                else  
                {  
                    mTime = value;  
                }  
            }  
        }   
        public override double Speed()  
        {  
            double speed = 0.0;  
            double hours = mTime / 60;  
            speed = mDistance / hours;  
            return speed;  
        }  
    }   
    class Program  
    {  
        static void Main(string[] args)  
        {  
            double speed = 0.0;  
            Car objCar = new Car();  
            Console.WriteLine("Enter the Distance");  
            objCar.Distance = Double.Parse(Console.ReadLine());  
            Console.WriteLine("Enter the time in minutes");  
            objCar.Time = Double.Parse(Console.ReadLine());  
            speed = objCar.Speed();  
            Console.WriteLine("Car speed is {0:0.00}", speed);  
            Console.Read();  
        }  
    }  
}  

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.  

Output

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#.


Similar Articles