C# Abstract Class and Abstract Method

What is Abstraction in C#

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

What is Abstract Class

An abstract class is a special type of class that cannot be instantiated. An abstract class is designed to be inherited by sub-classes that either implement or override its methods. In other words, abstract classes are either partially implemented or not implemented at all. You can have functionality in your abstract class—the methods in an abstract class can be both abstract and concrete. An abstract class can have constructors.

abstract class is a class that is declared with an abstract modifier. If we define a class with abstract modifier, then that class is intended only to be used as a base class for other classes.

The abstract class cannot be instantiated and it can contain both abstract and non-abstract members. The class that is derived from the abstract class must implement all the inherited abstract methods and accessors.

abstract class is defined by using abstract keyword.

Following is the example of defining an abstract class using abstract keyword.

abstract class Info

{

abstract public void GetDetails();

}

If you observe above code snippet, we defined an abstract class (Info) using abstract keyword with GetDetails method signature.

If we define a method with abstract modifier, then that method implementation must be done in a derived class.

Following is the example of implementing a class by deriving from the abstract class.

class User : Info
{
    public override void GetDetails()
    {
        // Method Implementation
    }
}

If you observe the code snippet, we inherited an abstract class (Info) in the User class and implemented a defined abstract method in the User class using override keyword. In c#, abstract methods are internally treated as virtual methods so those methods need to be overridden by the derived class.

In c#, we should not use a sealed keyword with abstract class because the sealed keyword will make a class as not inheritable but abstract modifier requires a class to be inherited.

C# Abstract Class Example

Following is the example of defining an abstract class using abstract modifier in c# programming language.

using System;

 

namespace Tutlane

{

    abstract class Info

    {

        abstract public void GetDetails(string x, string y, int z);

    }

    class User : Info

    {

        public override void GetDetails(string a, string b, int c)

        {

            Console.WriteLine("Name: {0}", a);

            Console.WriteLine("Location: {0}", b);

            Console.WriteLine("Age: {0}", b);

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            User u = new User();

            Console.WriteLine("****Abstract Class Example****");

            u.GetDetails("Suresh Dasari", "Hyderabad", 32);

            Console.ReadLine();

        }

    }

}

If you observe above example, we defined an abstract class called “Info” with required methods and the derived class “User” has implemented all the inherited abstract methods and accessors.

Output:

"****Abstract Class Example****"
Name:Suresh Dasari
Location:Hyderabad
Age:32

Important Points:

  1. Generally, we use abstract class at the time of inheritance.
  2. 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.
  3. An abstract class cannot be inherited by structures.
  4. It can contains constructors or destructors.
  5. It can implement functions with non-Abstract methods.
  6. It cannot support multiple inheritance.
  7. It can’t be static.
  8. In c#, abstract classes cannot be instantiated.
  9. The abstract classes can contain both abstract and non-abstract methods and accessors.
  10. In c#, we should not use a sealed keyword with abstract class because the sealed keyword will make a class as not inheritable but abstract modifier requires a class to be inherited.

Let’s See Another Example of abstract Class

// 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(); 
		
	} 
} 

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

// 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()); 
	} 
} 

Output:

Area  = 36

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

Example:

// 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)); 
	} 
} 

Output:

Addition : 10
Multiplication :24

Abstract class can also work with get and set accessors.

Example:

// 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); 
	} 
} 

Output:

5

C# Abstract Method

The abstract method is a method that is declared with an abstract modifier. If we define a method with abstract modifier, then that method doesn’t contain any implementation and method declaration simply ends with a semicolon.

Following is the example of defining an abstract method in c# .

public abstract void GetDetails();

The abstract methods in c# are permitted to declare only in abstract classes and the class that is derived from an abstract class must provide an implementation for defined abstract methods.

In c#, abstract methods are internally treated as virtual methods so those methods need to be overridden in the derived class and we should not static or virtual modifiers during abstract method declaration.

Example:

using System;
namespace Tutlane
{
    abstract class Info
    {
        public void Welcome()
        {
            Console.WriteLine("Welcome to Tutlane");
        }
        public int age = 32;
        public abstract void GetDetails(string x, string y);
    }
    class User : Info
    {
        public override void GetDetails(string a, string b)
        {
            Welcome();
            Console.WriteLine("Name: {0}", a);
            Console.WriteLine("Location: {0}", b);
            Console.WriteLine("Age: {0}", age);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            User u = new User();
            Console.WriteLine("****Abstract Class Example****");
            u.GetDetails("Suresh Dasari", "Hyderabad");
            Console.ReadLine();
        }
    }
}

If you observe the above example, we defined an abstract class called “Info” with required abstract and non-abstract methods and the derived class “User” has implemented all the inherited abstract methods and accessors.

When you execute the above c# program, we will get the result as shown below.

Output:

****Abstract Class Example****
Welcome to Tutlane
Name: Suresh Dasari
Location: Hyderabad
Age: 32

Important Points:

  • In c#, abstract methods are permitted to declare only within abstract classes.
  • The abstract method declaration will not contain any implementation, only the derived classes will provide an actual implementation for abstract methods.
  • In c#, abstract methods are internally treated as virtual methods so those methods need to be overridden in the derived class.
  • We should not use static or virtual modifiers during the abstract method declaration.

In c#, abstract properties will act the same as abstract methods but the only difference is declaration and invocation syntax. 

Abstract Properties

Difference between Abstraction and Encapsulation: