C# Abstract

In c#, abstract is a keyword which is used to define a classes and class members that are needs to be implemented or overridden in a derived class.

 

In c#, we can use abstract modifier with classes, methods, properties, events and indexers based on our requirements. The members which we defined as abstract or included in an abstract class those must be implemented by a classes that derive 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 which 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 abstract class must implement all the inherited abstract methods and accessors.

 

In c#, we 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 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 derived class.

 

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

 

class User : Info

{

    public override void GetDetails()

    {

        // Method Implementation

    }

}

If you observe code snippet, we inherited an abstract class (Info) in User class and implemented a defined abstract method in User class using override keyword. In c#, abstract methods are internally treated as virtual methods so those methods needs to be overridden by 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.

 

When we execute above c# program, we will get the result like 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

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 as not inheritable but abstract modifier requires a class to be inherited.
  • A class that is derived from abstract class must include all the implementations of inherited abstract methods and accessors.

C# Abstract Method

In c#, abstract method is a method which 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 semicolon.

 

Following is the example of defining an abstract method in 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 abstract class must provide an implementation for defined abstract methods.

 

In c#, abstract methods are internally treated as virtual methods so those methods needs to be overridden in 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 abstract class in 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 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 we execute above c# program, we will get the result like 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

Following are the important features of abstract method in 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 those methods needs to be overridden in derived class.
  • We should not static or virtual modifiers during abstract method declaration.

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

 
 

NEXT

C# Interface