What Is The Difference Between Interface And Abstract Class And When To Use It

Abstract class

An abstract class is a class where we can create abstract methods as well as concrete methods. An abstract class & abstract method should be declared using abstract keywords. An abstract class can have any number of abstract and non-abstract methods. We can't create instances of an abstract class but we can create instances of its subclasses. 

  • We can't create objects of abstract class
  • The abstract method must have without definition.
  • An abstract class can have a concrete method as well.
  • An abstract class can have without an abstract method.
  • An abstract class can never be sealed or static.
  • A concrete class can not inherit more than one abstract class. It means multiple inheritances is not possible in abstract classes.
  • It contains a constructor.
  • It can contain access modifiers.

Interface

An interface is similar to an abstract class but by default all methods are abstract. It means that it can't have concrete methods, It is just like method signature without implementation. We use the interface keyword to define an interface. We can't use access modifiers because by default is abstract.

  • We can't create an object of interface
  • Interface only contain method signature without definition.
  • We can't use access modifiers in the interface.
  • A concrete class can inherit more than one interface. It means multiple inheritances are possible in the interface.

When to use Abstract class 

If we know about implementation not completely but partially then we should go for abstract class. For example some common functionality like if we have a mobile phone of Samsung and iPhone. In mobile phones some common functionality is CALL and SMS but non-common functionality is color, screen size, and operating system. So here we can create class MobilePhone then can create a concrete method for CALL, SMS, and abstract method for color, screen size, and operating system. Let's begin with an example.

 class Program
    {
        static void Main(string[] args)
        {
            MobilePhone samsung = new Samsung();
            samsung.Call();
            samsung.Sms();
            samsung.Color();
            samsung.ScreenSize();
            samsung.OperatingSystem();

            Console.ReadLine();
        }
    }
    public abstract class MobilePhone
    {
        public void Call()
        {
            Console.WriteLine("Concrete Method:  You can make a call");
        }
        public void Sms()
        {
            Console.WriteLine("Concrete Method:  You can send a message");
        }

        public abstract void Color();
        public abstract void ScreenSize();
        public abstract void OperatingSystem();
    }
    public  class Samsung : MobilePhone
    {
        public override void Color()
        {
            Console.WriteLine("Abstract method:  This samsung mobile color is white");
        }
        public override void ScreenSize()
        {
            Console.WriteLine("Abstract method:  This samsung mobile screen size is 5 inch");
        }
        public override void OperatingSystem()
        {
            Console.WriteLine("Abstract method:  This samsung mobile android operating system");
        }
    }

The above example is for Samsung mobile and the below example for iPhone.

 class Program
    {
        static void Main(string[] args)
        {
            MobilePhone iPhone = new IPhone();
            iPhone.Call();
            iPhone.Sms();
            iPhone.Color();
            iPhone.ScreenSize();
            iPhone.OperatingSystem();

            Console.ReadLine();
        }
    }
    public abstract class MobilePhone
    {
        public void Call()
        {
            Console.WriteLine("Concrete Method:  You can make a call");
        }
        public void Sms()
        {
            Console.WriteLine("Concrete Method:  You can send a message");
        }

        public abstract void Color();
        public abstract void ScreenSize();
        public abstract void OperatingSystem();
    }

    public class IPhone : MobilePhone
    {
        public override void Color()
        {
            Console.WriteLine("Abstract method:  This IPhone color is white");
        }
        public override void ScreenSize()
        {
            Console.WriteLine("Abstract method:  This IPhone screen size is 5 inch");
        }
        public override void OperatingSystem()
        {
            Console.WriteLine("Abstract method:  This IPhone IOS");
        }
    }

You can have an abstract class without an abstract method.

What Is Difference Between Interface And Abstract Class And When To Use It

Multiple inheritances are not possible in abstract classes.

What Is Difference Between Interface And Abstract Class And When To Use It

You cannot use sealed or static in Abstract class.

What Is Difference Between Interface And Abstract Class And When To Use It

When to use Interface

If we don't know anything about implementation then we should go for the interface, I mean if we don't know the functionality and future implementation. If in future features will come then we can create a method signature according to that into the interface. When we need to decouple the class dependencies then we can go with interface. When we need multiple inheritance in our program then we should go with inheritance.

Multiple inheritance is possible,

 public interface ISamsung
    {
        void MethodA();
    }

    public interface IIphone
    {
        void MethodB();
    }

    public class ClassA : ISamsung, IIphone
    {
        public void MethodB()
        {
            throw new NotImplementedException();
        }

        void ISamsung.MethodA()
        {
            throw new NotImplementedException();
        }
    }

Example of Interface,

using System;

namespace Interface
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }

    public interface ISmartPhone
    {
        void Os();
        void AppStore();
    }

    public interface IIphone
    {
        void MethodB();
    }

    public class ClassA : IIphone
    {
        public void MethodB()
        {
            throw new NotImplementedException();
        }

    }

    public class Samsung : ISmartPhone
    {
        public void AppStore()
        {
            Console.WriteLine("Samsung mobile with android App store");
        }

        public void Os()
        {
            Console.WriteLine("Samsung with android OS");
        }
    }

    public class Iphone : ISmartPhone
    {
        public void AppStore()
        {
            Console.WriteLine("iPhone App store for IOS");
        }

        public void Os()
        {
            Console.WriteLine("iPhone with IOS");
        }
    }
}

Summary

So we can us interface and abstract based on our requirement like when we know about implementation then go for abstract class and when we don't know anything about implementation then we should go for interface.


Similar Articles