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
{
    class Program
    {
        static void Main(string[] args)
        {
            child ch = new child();
            ch.Describe();
            ch.getClassName();
         
        }
    }
 
    abstract class parent
    {
        public virtual void Describe()
        {
            Console.WriteLine("Hello Mindstick!");
            Console.ReadKey();
        }
        public abstract void getClassName();  
     //it shows error when we compine abstract body can not be define
        //public abstract void getClassName()
        //{
        //    Console.WriteLine("manish");
        //}
      
    }
 
    class child : parent
    {
 
        public override void 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
{
    class Program
    {
        static void Main(string[] args)
        {
            child ch = new child();
            ch.Describe();
          
         
        }
    }
 
    interface  parent
    {
       void Describe();
       
      
    }
 
   public class child:parent
    {
       public void 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#

  Modified On Mar-17-2018 01:13:53 AM

Leave Comment