blog

Home / DeveloperSection / Blogs / Abstract and Interface in c#

Abstract and Interface in c#

Elena Glibart2407 17-Jan-2017

Abstract-An Abstract Class can have non-abstract method (Concrete Method) and abstract method .It Allows Access Specifier .We cannot make instance of abstract Class. If we have to access the method of abstract class then we have to make child class and then we have to inherit the abstract class then by making the instance of child class we can access. We use abstract keyword for declaring Abstract Class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication1
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            child ch = newchild();
            ch.Describe();
            ch.getClassName();
         
        }
    }
 
    abstractclassparent
    {
        publicvirtualvoid Describe()
        {
            Console.WriteLine("Hello Mindstick!");
            Console.ReadKey();
        }
        publicabstractvoid getClassName();  
     //it shows error when we compine abstract body can not be define
        //public abstract void getClassName()
        //{
        //    Console.WriteLine("manish");
        //}
      
    }
 
    classchild : parent
    {
 
        publicoverridevoid getClassName()
        {
            Console.WriteLine("aditya kumar");
            Console.ReadKey();
        }
    }
}


Abstract and Interface in c#


Interface- Interface contain only declaration of method, Properties, But it cannot contain its implementation part. The responsibility of implementation is of child class. Interface does not contain any specifier. Interface does not contain any constructor if we make any constructor it shows error.We use interface keyword for declaring interface

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication1
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            child ch = newchild();
            ch.Describe();
          
         
        }
    }
 
    interface  parent
    {
       void Describe();
       
      
    }
 
   publicclasschild:parent
    {
       publicvoid Describe()
        {
            Console.WriteLine("hello");
            Console.ReadKey();
        }
 
       
    }
}



Abstract and Interface in c#


Difference between Abstract and Interface       

Interface support multiple inheritance but abstract cannot support multiple inheritance in c#.

In Interface we cannot declare its body part but abstract class can contain its body part means implementation part.

We can make instance of abstract class but we cannot make instance of interface

You can also visit these related articles & blogs

Abstraction in c# with example

Interface in c#

Abstract class in c#

Difference Between interface vs Abstract class in c#


Updated 17-Mar-2018

Leave Comment

Comments

Liked By