Abstract Classes and Interface in C#


Abstract classes, marked by the keyword abstract in the class definition, are typically used to define a base class in the hierarchy. You can't create an instance of abstract classes.  Class members that are incomplete/abstract and must be implemented in a derived class.

       Example:- 
           abstract class MyAbstractClass
        {
            //You can have methods here, with/without implementation.
            // An abstract method cannot be private.
            public abstract void DoWork(int i);
        }
  • Abstract classes are closely related to interfaces. They are classes that cannot be instantiated, and are frequently either partially implemented, or not at all implemented.
  • An abstract class can contain either abstract methods or non-abstract methods. Abstract members do not have any implementation in the abstract class, but the same has to be provided in its derived class.
  • An abstract class cannot be a sealed class as they are mean to be for inheritance.
  • An abstract method cannot be private.
  • The access modifier of the abstract method should be same in both the abstract class and its derived class. 
  • An abstract method cannot have the modifier virtual. Because an abstract method is implicitly virtual.
  • Defining an abstract class with abstract members has the same effect to defining an interface.


Interface: - An interface contains definitions for a group of related functionalities that a class or a struct can implement. Interface is a contract that defines the signature of the functionality. So if a class is implementing interface it says to the outer world, that it provides specific behavior. The only thing it contains are declarations of events, indexers, methods and/or properties. 
Example: - If a class is implementing 'Idisposable' interface that means it has a functionality to release unmanaged resources. Now external objects using this class know that it has contract by which it can dispose unused unmanaged objects. 
  • Single Class can implement multiple interfaces.
  • If a class implements an interface then it has to provide implementation to all its methods.

Interfaces in C # provide a way to achieve runtime polymorphism. Using interfaces we can invoke functions from different classes through the same Interface reference, whereas using virtual functions we can invoke functions from different classes in the same inheritance hierarchy through the same reference. 

Technically, the interface only governs syntax, i.e. what methods are there, what arguments they get and what they return. 

Example:-


         class Demo : abc
        {
            public static void Main()
            {
                System.Console.WriteLine("Hi Interfaces");
                Demo refDemo = new Demo();
                refDemo.xyz();
                Sample refSample = new Sample();
                refSample.xyz();
            }

            public void xyz()
            {
                System.Console.WriteLine("In Demo");
            }
        }

        interface abc
        {
            void xyz();
        }

        class Sample : abc
        {
            public void xyz()
            {
                System.Console.WriteLine("In Sample");
            }

        } 

Output:-
          In Demo
          In Sample 



Multiple Inheritance can be achieved using interfaces below code example demonstrate the same:-

Example:-


        //Creating Interface
        interface IA
        {
            void A();
        }

        interface IB
        {
            void B();
        }
        //Creating class which will inherits from multiple iunterfaces
        class Demo : IA, IB
        {
            public static void Main()
            {
                System.Console.WriteLine("Hello");
                Demo refDemo = new Demo();
                IA refA = refDemo;
                refA.A();
                IB refB = refDemo;
                refB.B();
            }

            public void A()
            {
                System.Console.WriteLine("In A");
            }

            public void B()
            {
                System.Console.WriteLine("In B");
            }

        }

Output:-
          In A
          In B


Difference between Abstract classes and Interfaces:-
  • Abstract classes can have concrete methods while interfaces have no methods implemented.
  • An abstract class can have abstract member's as well non abstract members. But in an interface all the members are implicitly abstract and all the members of the interface must override to its derived class.
  • Interfaces do not come in inheriting chain, while abstract classes come in inheritance.
  • The members of the interface are public with no implementation. Abstract classes can have protected parts, static methods, etc.
  • A class can inherit one or more interfaces, but only one abstract class.

Difference between Abstraction and Encapsulation:-
  •  Abstraction is providing a generalization (say, over a set of behaviors). Abstraction lets you focus on what the object does instead of how it does it, provide a common way to work with objects.
  •  Encapsulation is hiding the implementation details which may or may not be for generic or specialized behavior(s).

Comments