Search This Blog

Monday, June 7, 2021

Abstract Class Vs Interface

 Interface

  • Completely "abstract class", which can only contain abstract methods and properties (with empty bodies).
  • By default, members of are abstract and public.
  • Can contain properties and methods, but not fields.
  • The body must be provided by the "derived" class without using override keyword.
  • Virtual method is not allowed in interface.
  • Cannot contain a constructor.
  • Multiple inheritance can achieved.
  • It can't be instantiate.

Abstract Class

  • It is Restricted class, that cannot be instantiate.
  • To access it, it must be inherited from another class.
  • Typically used to define a base class in the class hierarchy.
  • An abstract class can have both abstract and regular methods.

Wrong

  • This class must contain at least one abstract method.
  • Abstract classes are either partially implemented or not implemented at all.

eg.

public interface IInterface // sealed, private, protected, protected internal are not possible, public is optional (by default public)
{
        void demo();
        void demo1(); 
}
public class InterfaceDemoTest : IInterface
{
        public void demo()
        {
            Console.WriteLine("Demo Method Called");
        }
        public void demo1()
        {
            Console.WriteLine("Demo1 Method Called");
        }
}
public abstract class AbstractClass // sealed, private, protected, protected internal are not possible
{
        int x, y;
        public AbstractClass()
        {
            Console.WriteLine("Abstract Constructor Called");
        }

        // Abstract method
        // Without abstract method is also possible
        public abstract void demo();

        // Concrete method
        public virtual void demo1()
        {
            Console.WriteLine("Non-Abstract Parent Method Called");
        }
    }

    public class AbstractDemoTest: AbstractClass
    {
        public override void demo()
        {
            Console.WriteLine("Abstract Method Called");
            AbstractClass abs = new AbstractDemoTest(); // new AbstractClass() is not possible
        }
}
class Program
{
        static void Main(string[] args)
        {
            AbstractDemoTest abstractDemoTest = new AbstractDemoTest();
            abstractDemoTest.demo();
            abstractDemoTest.demo1();

            InterfaceDemoTest interfaceDemoTest = new InterfaceDemoTest();
            interfaceDemoTest.demo();
            interfaceDemoTest.demo1();

            IInterface iinterface = new InterfaceDemoTest();
            iinterface.demo();
            iinterface.demo1();

            Console.ReadLine();
}
}

Difference between Abstract class and Interface

Abstract

Interface

Can have abstract and regular(concrete) methods both

Can have only abstract methods without abstract keyword

Can have Constructor

Can’t have Constructor

Can create object as reference to child class

AbstractClass abs = new AbstractDemoTest();

Can create object as reference to child class

IInterface iinterface = new InterfaceDemoTest();

By default private class

By default public interface

Access modifiers required for methods

Access modifiers not required for methods

Multiple inheritances can’t achieve.

Multiple inheritances can achieve.

Can contain fields

Can’t contain fields

Only Abstract methods must be implement in derived class

All methods must be implement in derived class

Used for inheritance

Used for inheritance


Similarities between Abstract class and Interface

Can’t Create Instance

Abstract methods must me implement in derived class

Used for inheritance

Used as Parent in inheritance