Monday 5 July 2021

Difference between Abstract Class and Interface in C# | Abstract Class Vs Interface | OOPS concept| Abstraction and Inheritance

 

Abstract Class

Interface

An abstract class is a way to achieve the abstraction in C#.

The Abstract classes are typically used to define a base class in the class hierarchy.

An Abstract class is never intended to be instantiated directly.

This class must contain at least one abstract method, which is marked by the keyword or modifier abstract in the class definition.

Interface can have methods, properties, events, and indexers as its members. Interfaces will contain only the declaration of the members.

The implementation of interface’s members will be given by the class who implements the interface implicitly or explicitly.

Interface cannot be instantiated

The abstract keyword is used to declare abstract class.

The interface keyword is used to declare interface

Abstract class doesn't support multiple inheritance. A subclass can at most use only one abstract class

Interface supports multiple inheritance. A class can implement any number of interfaces

Abstract class can have abstract and non-abstract methods.

Interface can have only abstract methods.

An abstract class can declare or use any variables

Interfaces cannot contain fields.

An abstract class can have constructor declaration

Interfaces cannot contain constructors

It can contain static members.

It does not contain static members.

An abstract Class is allowed to have all access modifiers for all of its member declaration 

 In interface we can’t declare any access modifier (including public) as all the members of interface are implicitly public.

The performance of an abstract class is fast.

The performance of interface is slow because it requires time to search actual method in the corresponding class.

It is used to implement the core identity of class.

It is used to implement peripheral abilities of class.

If many implementations are of the same kind and use common behaviour, then it is superior to use abstract class.

If many implementations only share methods, then it is superior to use Interface.

Abstract class can contain methods, fields, constants, etc.

Interface can only contain methods.

It can be fully, partially or not implemented.

It should be fully implemented.

It contains both declaration and definition part

It contains only a declaration part.

Example:

public interface SampleInterface

    {

        void FirstMethod();

        string SecondName();

    }

Example:

abstract class SampleAbstractClass

    {

        int variable1 = 0;

        int variable2 = 1;

        public abstract void FirstMethod();

    }


Difference between abstract class and interface in C#




4 comments:

  1. Really I enjoy your site with effective and useful information. It is included very nice post with a lot of our resources. thanks for share. i enjoy this post.

    ReplyDelete
  2. Great Information, thank you for your valuable information.
    Best Application Development Company/a>

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...

ShareThis