Differences Between Interface and Abstract Class

  • OOP
You are currently viewing Differences Between Interface and Abstract Class

Differences Between Interface and Abstract Class

In this article, I will be talking about the differences between Interface and Abstract Class. But firstly, let’s take a closer look at both terms, and then compare them.

What is Abstraction?

Objects cannot be created from classes created with the abstract keyword, they can only be inherited. Methods, events and properties created with the Abstract keyword can only be defined in Abstract classes and have no content. Its body is provided by inherited classes.

Abstraction in Software is the process of showing some of the details while hiding some. It is one of the OOP (Object Oriented Programming) techniques used to make software architecture more useful and understandable. Abstraction can be used in classes, methods, events and properties.

namespace InterfaceAndAbstractTest
{
    //Abstract Class
    public abstract class Animal
    {
        //Abstract Property
        public abstract string Name { get; }
        
        //Abstract Event
        public abstract event Action<string> OnSpeak;

        //Abstract Method
        public abstract void Speak();

        //Constructor Method
        protected Animal()
        {
            OnSpeak += OnSpoke;
        }

        //Private method
        private void OnSpoke(string words)
        {
            Console.WriteLine(words);
        }
    }

    public class Dog : Animal
    {
        public override string Name => "Scooby-Doo";

        public override event Action<string> OnSpeak;

        public override void Speak()
        {
            OnSpeak?.Invoke($"{Name} - Hav hav!");
        }
    }

    public class Cat : Animal
    {
        public override string Name => "Garfield";

        public override event Action<string> OnSpeak;

        public override void Speak()
        {
            OnSpeak?.Invoke($"{Name} - Meow!");
        }
    }
    
    class Program
    {
        static void Main()
        {
            Animal dog = new Dog();
            dog.Speak();

            Animal cat = new Cat();
            cat.Speak();
        }
    }
}

The output is as shown below.

Scooby-Doo - Hav hav!
Garfield - Meow!

In this example, we see 2 subclasses that derive from the Abstract Animal class. Abstract method, abstract event and abstract properties defined in abstract class must also be defined and filled in subclasses. Each of the subclasses performs its own operation within the framework it defines. The bodies of non-abstract definitions of an Abstract class can be filled in the Abstract class, but the bodies of abstract definitions can only be filled in subclasses.

What is Interface?

It is a structure that provides common features between classes and brings classes together at common points. All of the definitions made in this structure must be filled in the implemented subclasses. Like Abstract classes, objects cannot be created with the new keyword from Interface.

namespace InterfaceAndAbstractTest
{
    public interface IBehaviors
    {
        void Speak();
        void Walk();
        void Sleep();
    }

    public class Dog : IBehaviors
    {
        public void Speak()
        {
            Console.WriteLine("Hav hav!");
        }

        public void Walk()
        {
            Console.WriteLine("Dog - Walking!");
        }

        public void Sleep()
        {
            Console.WriteLine("Dog - Sleeping!");
        }
    }

    public class Cat : IBehaviors
    {
        public void Speak()
        {
            Console.WriteLine("Meow!");
        }

        public void Walk()
        {
            Console.WriteLine("Cat - Walking!");
        }

        public void Sleep()
        {
            Console.WriteLine("Cat - Sleeping!");
        }
    }
    
    class Program
    {
        static void Main()
        {
            IBehaviors dog = new Dog();
            dog.Speak();
            dog.Walk();
            dog.Sleep();
            
            IBehaviors cat = new Cat();
            cat.Speak();
            cat.Walk();
            cat.Sleep();
        }
    }
}

The output is as shown below.

Hav hav!
Dog - Walking!
Dog - Sleeping!
Meow!
Cat - Walking!
Cat - Sleeping!

In this example, we see 2 classes that implement the IBehaviors interface. All definitions defined in interfaces must be defined and populated in the implementing classes. Each of the classes that implemented performs its own operation within the framework it defines.

What are the differences between Interface and Abstract Class?

We talked about both techniques. Now we can list the differences.

ABSTRACT CLASSINTERFACE
Can be inherited from abstract classInterface can only be implemented.
One Abstract class can be inherited from a class.A class can implement more than one Interface.
Both other Classes can be inherited and Interfaces can be implementedOnly Interfaces can be implemented.
The constructor can contain a method.The constructor cannot contain a method.
Different types of access specifiers can be used.Every defined method is declared as public.
Both virtual methods and normal methods can be defined in it.Only virtual methods can be defined in it.
Abstract class can contain method, field, property etc.Interface can only contain method signatures.

Leave a Reply