C# | Abstract Classes

Abstraction in C# is the process to hide the internal details and showing only the functionality. The abstract modifier indicates the incomplete implementation. The keyword abstract is used before the class or method to declare the class or method as abstract. Also the abstract modifier can be used with indexers, events and properties.

Example:

public abstract void geek();
// this indicates the method 'geek()' is abstract
 
abstract class gfg
// this indicates the class 'gfg' is abstract

Abstract Method: A method which is declared abstract, has no “body” and declared inside the abstract class only.

Syntax:

public abstract void geek();
// the method 'geek()' is abstract

Abstract Class: This is the way to achieve the abstraction in C#. 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. The Abstract classes are typically used to define a base class in the class hierarchy.



Syntax:

abstract class gfg{}
// class 'gfg' is abstract

Important Points:

  • Generally, we use abstract class at the time of inheritance.
  • A user must use the override keyword before the method which is declared as abstract in child class, the abstract class is used to inherit in the child class.
  • An abstract class cannot be inherited by structures.
  • It can contains constructors or destructors.
  • It can implement functions with non-Abstract methods.
  • It cannot support multiple inheritance.
  • It can’t be static.

Example 1: Program to show the working of an abstract class

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to show the 
// working of abstract class
using System;
  
// abstract class 'GeeksForGeeks'
public abstract class GeeksForGeeks {
  
    // abstract method 'gfg()'
    public abstract void gfg();
      
}
  
// class 'GeeksForGeeks' inherit
// in child class 'Geek1'
public class Geek1 : GeeksForGeeks
{
  
    // abstract method 'gfg()' 
    // declare here with 
    // 'override' keyword
    public override void gfg()
    {
        Console.WriteLine("class Geek1");
    }
}
  
// class 'GeeksForGeeks' inherit in 
// another child class 'Geek2'
public class Geek2 : GeeksForGeeks
{
  
    // same as the previous class
    public override void gfg()
    {
        Console.WriteLine("class Geek2");
    }
}
  
// Driver Class
public class main_method {
  
    // Main Method
    public static void Main()
    {
  
        // 'g' is object of class
        // 'GeeksForGeeks' class '
        // GeeksForGeeks' cannot
        // be instantiate
        GeeksForGeeks g;
  
        // instantiate class 'Geek1'
        g = new Geek1();
          
        // call 'gfg()' of class 'Geek1'
        g.gfg();
        
        // instantiate class 'Geek2'  
        g = new Geek2();
        
        // call 'gfg()' of class 'Geek2'
        g.gfg();
          
    }
}

chevron_right


Output:

class Geek1
class Geek2

Example 2: Program to calculate the area of a square using abstract class and abstract method

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to calculate the area 
// of a Square using abstract class
// and abstract method
using System;
  
// declare class 'AreaClass'
// as abstract
abstract class AreaClass
{
    // declare method 
    // 'Area' as abstract
    abstract public int Area();
}
  
// class 'AreaClass' inherit
// in child class 'Square'
class Square : AreaClass
{
    int side = 0;
  
    // constructor
    public Square(int n)
    {
        side = n;
    }
  
    // the abstract method 
    // 'Area' is overridden here
    public override int Area()
    {
        return side * side;
    }
}
  
class gfg {
  
    // Main Method
    public static void Main()
    {
        Square s = new Square(6);
        Console.WriteLine("Area  = " + s.Area());
    }
}

chevron_right


Output:

Area  = 36

Following are some important observations about abstract classes in C#

1) An Abstract class does not mean that it only contain abstract methods. An Abstract class can also contain non-abstract methods also.

Syntax:

abstract class gfg
{
    public void geek()
    {
        Console.WriteLine("'geek()' is non-abstract method");
    }
}

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to show the working of 
// the non-abstract method in the 
// abstract class
using System;
  
abstract class AbstractClass {
  
    // Non abstract method
    public int AddTwoNumbers(int Num1, int Num2)
    {
        return Num1 + Num2;
    }
  
    // An abstract method which 
    // overridden in the derived class
    public abstract int MultiplyTwoNumbers(int Num1, int Num2);
      
}
  
// Child Class of AbstractClass
class Derived : AbstractClass {
  
    // implementing the abstract 
    // method 'MultiplyTwoNumbers'
    // using override keyword,
    public override int MultiplyTwoNumbers(int Num1, int Num2)
    {
        return Num1 * Num2;
    }
}
  
// Driver Class
class geek {
  
    // Main Method
    public static void Main()
    {
   
        // Instance of the derived class
        Derived d = new Derived();
  
        Console.WriteLine("Addition : {0}\nMultiplication :{1}",
                                          d.AddTwoNumbers(4, 6),
                                    d.MultiplyTwoNumbers(6, 4));
    }
}

chevron_right


Output:

Addition : 10
Multiplication :24

2) Abstract class can also work with get and set accessors.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to show the working 
// of abstract class with the 
// get and set accessors
using System;
  
abstract class absClass {
  
    protected int myNumber;
  
    public abstract int numbers
    {
        get;
        set;
    }
}
  
class absDerived : absClass {
  
    // Implementing abstract properties
    public override int numbers
    {
        get
        {
            return myNumber;
        }
        set
        {
            myNumber = value;
        }
    }
}
  
// Driver Class
class geek {
  
    // Main Method
    public static void Main()
    {
        absDerived d = new absDerived();
        d.numbers = 5;
        Console.WriteLine(d.numbers);
    }
}

chevron_right


Output:

5


My Personal Notes arrow_drop_up

2nd year student of Information Technology JADAVPUR UNIVERSITY

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.