C# Abstract

In c#, abstract is a keyword, and it is useful to define classes and class members that are needed to be implemented or overridden in a derived class.

 

In c#, you can use abstract modifiers with classes, methods, properties, events, and indexers based on our requirements. The members we defined as abstract or included in an abstract class must be implemented by classes derived from an abstract class.

 

Now we will see how to use abstract modifier in classes and methods with examples.

C# Abstract Class

In c#, abstract class is a class that is declared with a 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 instantiate, 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.

 

In c#, you can define an abstract class 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 the 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 the 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 an abstract class because the sealed keyword will make a class 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 the 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.

 

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

 

C# Abstract Class Example Result

 

This is how we can use abstract classes in our applications based on our requirements.

C# Abstract Class Features

The following are important features of abstract class in c# programming language.

 

  • In c#, abstract classes cannot be instantiated.
  • The abstract classes can contain both abstract and non-abstract methods and accessors.
  • In c#, we should not use a sealed keyword with abstract class because the sealed keyword will make a class not inheritable, but abstract modifier requires a class to be inherited.
  • A class that is derived from an abstract class must include all the implementations of inherited abstract methods and accessors.

C# Abstract Method

In c#, the abstract method is a method that is declared with a abstract modifier. If we define a method with abstract modifier, then that method doesn’t contain any implementation, and method declaration ends with a semicolon.

 

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

 

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. Hence, those methods need to be overridden in the derived class, and we should not static or virtual modifiers during abstract method declaration.

C# Abstract Method Example

Following is the example of declaring an abstraction method in an abstract class in the c# programming language.

 

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. 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.

 

C# Abstract Methods with Examples

 

This is how we can use abstract methods in c# abstract classes based on our requirements.

C# Abstract Method Features

The following are the important features of the abstract method in the c# programming language.

 

  • 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 they 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.