Abstract classes in C-Sharp in detail with example  

Abstract class is an incomplete class i.e. it contains atleast one abstract method. It cannot be instantiated. It can have abstract and non-abstract methods. In C#, abstract class is declared abstract by keyword abstract.

0 446 Technology | C-Sharp
Tue Dec 25 , 2018

Abstract classes are the way to achieve abstraction in C#. Abstraction is the process to hide the internal details and showing functionality only. It is must to inherit the abstract class.


Syntax: Abstract class declaration


<abstract> class <abstractClassName>
{
  public <abstract> void <abstractMethodName()>;
}


Important Points

  • An abstract class cannot be a sealed class i.e. the following declaration is incorrect.
    //Incorrect
    abstract sealed class absClass
    {
    }
    
  • An abstract method cannot be private.
    //Incorrect
    private abstract int Sum();
    
    The access modifier of the abstract method should be same in both the abstract class and its derived class. If you declare an abstract method as protected in the abstract class then it should be protected in the class which is inheriting the abstract class. Otherwise, the compiler will raise an error.
  • An abstract method cannot be static.
    //Incorrect
    public abstract static int Sum();
    
  • An abstract class cannot support multiple inheritance.
     //An abstract class cannot support multiple inheritance
        class X
        {
        }
        class Y : X
        {
        }
        abstract class Container : Y //But we can't inherit like this : X,Y
        {
        }
    
  • An abstract class cannot be inherited from by structures.
     //An abstract class cannot be inherited from by structures
        public struct test
        {
        }
        //We can't inheritance the struct class on the abstract class
        abstract class NotInheritanceStruct
        {
        }
    
  • An abstract class can have constructors or destructors.
    //An abstract class can have constructors or destructors
        abstract class ConsDes
        {
            ConsDes()
            {
            }
            ~ConsDes()
            {
            }
        }
    
  • An Abstract class can have modifiers for methods, properties etc.
    public abstract class abstractModifier
    {
        private int id;
        public int ID
        {
            get { return id; }
            set { id = value; }
        }
        internal abstract void Sum();
    } 
    

Example

 
    using System;
    public abstract class Addition  
    {  
        public abstract int Sum(int no1,int no2);  
    }  
    public class TestAbstract: Addition   
    {  
     	public override int Sum(int no1,int no2)
	{
	 int no3=no1+no2;
         Console.WriteLine("Addition of 2 no's : "+no3);
	 return no3;
	}
        public static void Main()  
        {  
            TestAbstract ta=new TestAbstract()  ;  
           ta.Sum(10,20);
        }  
    }   

Output

 
Addition of 2 no's : 30


Subscribe & Get Regular Updates On Your E-mail