Skip to main content

Difference between Abstract Class and Interface with example in c#


1. Multiple inheritance

 A class may inherit several interfaces.
 class abc : ICrud,IDisposal

 A class may inherit only one abstract class.
 class abc : parentClass

2. Default implementation

Interface: An interface cannot provide any code, just the signature.
Example:

interface IPerson
    {
        void Add(string name, string email);
    }
   
Abstract class: An abstract class can provide complete, default code and/or just the details that have to be overridden.
Example:

abstract class APerson
    {
        //
        string Name;
        string Email;

       // like interface
       public abstract void Add(string name, string email);
      
       public void Update()
       {

       }

    }

3. Access Modifiers


Interface: An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public

    interface IPerson
    {
        void Add(string name, string email);
    }

    class iPerson : IPerson
    {
        string Name;
        string Email;

        public void Add(string name, string email)
        {
            Email = email;
            Name = name;
        }
    }

Abstract Class: An abstract class can contain access modifiers for the subs, functions, properties

abstract class APerson
    {
        //
        protected string Name;
        protected string Email;

        // like interface
        protected abstract void Add(string name, string email);

        protected void Update()
        {

        }

    }

    class person : APerson
    {
        protected override void Add(string name, string email)
        {
            Email = email;
            Name = name;
        }
    }

4. Where to Use

Interface: If various implementations only share method signatures then it is better to use Interfaces.

Abstract Class: If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.

5. Speed

Interface: Requires more time to find the actual method in the corresponding classes.

Abstract Class: Faster then Interface

Comments

Popular posts from this blog

Types Of Join In SQL

Sql joins are used to fetch/retrieve data from two or more data tables, based on a join condition. A join condition is a relationship among some columns in the data tables that take part in Sql join. Basically data tables are related to each other with keys. We use these keys relationship in sql joins. Inner Join Outer Join Cross Join Cross Join

Change Key Dynamically in Web.Config

System.Xml; private void SetConfigSettings() { string path = Server.MapPath("Web.config"); string newConnectionString = @"Server=local;Database="+txtDatabaseName.Text+";Trusted_Connection=true"; XmlDocument xDoc = new XmlDocument(); xDoc.Load(path); XmlNodeList nodeList = xDoc.GetElementsByTagName("appSettings"); XmlNodeList nodeAppSettings = nodeList[0].ChildNodes; XmlAttributeCollection xmlAttCollection = nodeAppSettings[0].Attributes; xmlAttCollection[0].InnerXml = txtKey.Text; // for key attribute xmlAttCollection[1].InnerXml = newConnectionString; // for value attribute xDoc.Save(path); // saves the web.config file }