Signup/Sign In
LAST UPDATED: SEPTEMBER 1, 2021

C# Abstract Class and Interface

    The objective of this article is to understand the concept of the Abstract class and Interface. Abstraction, Encapsulation, Inheritance, and Polymorphism are the four pillars of object-oriented programming. In this article, we will also introduce you to how to achieve multiple inheritance using interfaces. Please read our previous articles on types of inheritance in C# if you have not already done so.

    Abstraction means representing essential data (relevant data of an object) and hiding the details and this is achieved using Abstract class and Interface in C# language. We will also cover the differences between an Abstract class and Interface in C# programming language.


    C# Abstract Class

    An abstract class declared is using the keyword abstract and can have abstract and non-abstract methods. The abstract methods must be declared inside the abstract class only and it must be implemented in non-abstract classes using the override keyword. An abstract class is an incomplete class that can't be instantiated (unable to create the object).

    If a class has an abstact method then that class must be made abstract else you will get compilation error.

    The following is the syntax of abstract class:

    public abstract class class_name
    {
            public void abstract method_name();
    }

    Note: The abstract modifier can be used with classes, methods, properties, etc.

    Characteristics of Abstract Class and Method:

    1. Abstract class can't be instantiated (unable to create the object).

    2. Abstract class doesn't support multiple inheritance.

    3. Abstract class can't be inherited from structures.

    4. An abstract class can have constructors or destructors.

    5. An abstract class can inherit from a class and one or more interfaces.

    6. An abstract method is by default a virtual method.

    7. Abstract methods must be overridden by the derived class.

    8. An abstract method has no implementation (derived class has its implementation).

    9. One can't use an abstract modifier along with static, virtual, and override modifiers.

    Let's take a practical implementation of abstract class.

    Filename: Program.cs

    using System;
    public abstract class Bank
    {
        public abstract void withdraw();
    }
    
    public class YesBank:Bank
    {
        public override void withdraw()
        {
            Console.WriteLine("Withdrawing cash from YesBank");
        }
    }
    
    public class NoBank:Bank
    {
        public override void withdraw()
        {
            Console.WriteLine("Withdrawing cash from NoBank");
        }
    }
    
    namespace Studytonight
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                Bank b = new YesBank();
                b.withdraw();
                b = new NoBank();
                b.withdraw();
            }
        }
    }

    Output:

    Withdrawing cash from YesBank
    Withdrawing cash from NoBank

    Firstly, we declared an abstract method withdraw() inside an abstract class and then implemented it in derived classes, YesBank and NoBank. If you closely observe these classes, both have different implementations of the withdraw() method. At last, we created an object of the Bank class and assigned a reference of YesBank and NoBank to it respectively.

    Now let's see another example of an abstract class where the implementation of the base class method is used in a derived class using virtual and base keywords. The abstract methods have no implementation, hence we implemented our method using the virtual keyword and it is unlike the abstract method. It has method body implementation, and it is not compulsory to override in the derived class. Also, the derived classes have different implementations of the withdraw() method, just like in normal case of inheritance.

    Filename: Program.cs

    using System;
    public abstract class Bank
    {
        public virtual void withdraw()
        {
            Console.WriteLine("Thank You. Bonus 200 added to your account");
        }
    }
    
    public class YesBank:Bank
    {
        public override void withdraw()
        {
            Console.WriteLine("Withdrawn cash from YesBank");
            base.withdraw();
        }
    }
    
    public class NoBank:Bank
    {
        public override void withdraw()
        {
            Console.WriteLine("Withdrawn cash from NoBank");
        }
    }
    
    namespace Studytonight
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                Bank b = new YesBank();
                b.withdraw();
                b = new NoBank();
                b.withdraw();
            }
        }
    }

    Output:

    Withdrawn cash from YesBank
    Thank You. Bonus 200 added to your account
    Withdrawn cash from NoBank

    Also, an abstract class can have normal methods, virtual methods, abstract methods in it. If a class has even a single abstract method then that class must be made abstract.

    The abstarct classes are generally used when we want to define some common features which can be used by all the derived classes and we want to make implementation of some methods mandatory so we make those methods abstract.


    C# Interface

    As we know, achieving multiple inheritance is not possible with classes, but it is possible with the help of interfaces using the interface keyword. The interface doesn't provide any code implementation, but it contains only the signatures of methods, properties, events or indexers. A class may inherit several interfaces, and an interface can inherit from one or more base interfaces.

    The following is the syntax of interface:

    access_modifier interface interface_name
    {
        // declaration of properties, methods etc with no definition
    }

    Let's take an implementation of an interface

    Filename: Program.cs

    using System;
    public interface Bank
    {
        // method signature declaration
        void withdraw();
    }
    
    public class YesBank:Bank
    {
        public void withdraw()
        {
            Console.WriteLine("Withdrawing cash from YesBank");
        }
    }
    
    public class NoBank:Bank
    {
        public void withdraw()
        {
            Console.WriteLine("Withdrawing cash from NoBank");
        }
    }
    
    namespace Studytonight
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                Bank b = new YesBank();
                b.withdraw();
                b = new NoBank();
                b.withdraw();
            }
        }
    }

    Output:

    Withdrawing cash from YesBank
    Withdrawing cash from NoBank

    Firstly, we declared a withdraw() method in the Bank interface and then its implementation is provided in YesBank and NoBank. If you closely observe the withdraw() method in the Bank interface, neither we used any access modifier such as public, private, etc. nor we implemented the method. If you implement it, the compiler will throw an error saying interface members cannot have a definition.


    Difference between Abstract Class and Interface

    Abstract Class Interface
    Abstract class doesn't support multiple inheritance. Interface support multiple inheritance.
    Abstract class contains constructors. Interface doesn’t contain constructors.
    Abstract class can have access modifier for methods. Interface is public by default and it can’t have access modifier for methods.
    Abstract class can provide implementation. Interface doesn’t provide implementation but it can have a signature.
    Abstract class can have non-abstract methods. Interface has only abstract methods.
    A class may inherit only on abstract class. A class may inherit several interfaces.

    We hope this article helped you to understand what is an abstract class and interface in C# language. If you have any questions about this topic, please let us know in the comment section. We are happy to solve your doubts.

    You may also like:

    Subject Matter Expert of C# Programming at Studytonight.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS