Monday, November 1, 2021

C# Abstract Class and Abstract Method



Abstract method

Method of a class is used to define the behavior of the class. Abstract method is a method that has no implementation of its behavior. In other words, the abstract method does not have body. In the body of a method, its behavior is defined. As abstract method is without body, it misses its behavior. Implementing a method means defining the behavior of the method. An abstract method has only method signature and 'abstract' modifier is used with it. for example
public abstract double Add(double number1, double number2);
public abstract double Subtract(double number1, double number2);
public abstract double Multiply(double number1, double number2);
public abstract double Divide(double number1, double number2); 

Abstract class

Abstract class and abstract method are related to each other. As we know that in object-oriented programming, method is a member of class; if a method in the class is abstract method then the class must be an abstract class. In other words, class containing an abstract member will be abstract class. It is very logical because the presence of abstract method implies that some behavior of the class is still missing and not defined. Obviously, such class must be abstract.

But an abstract class can be without any abstract member as well. In this case, we recognize the abstract class with the abstract modifier used with the class.
public abstract class AbsClass
{
// It's an abstract class
}
Example of abstract class with an abstract method:
public abstract class AbsClass
{
   public abstract void Display();
   public void Message()
   {
    Console.WriteLine("Non-abstract method");
  }
}
The most important point is that an instance of an abstract class cannot be created. For example the following statement is wrong.
AbsClass obj = new AbsClass();
There can be both abstract and non-abstract methods within an abstract class. For example the following abstract class has both abstract and non abstract methods.

public abstract class AbsClass
{
public abstract void Display();
public void Message()
{
Console.WriteLine("Non-abstract method");
}
}
Unless all the methods of the abstract class are implemented, the abstract class cannot be consumed except for static methods. For example, the abstract method Display of the above AbsClass class is implemented in the following code in the Child class, which inherits AbsClass.
public abstract class Child : AbsClass
{
public abstract void Display()
{
Console.WriteLine("Display method is implemented.");
}
}
To consume an abstract class, its child class must implement all the abstract methods of the abstract class. If a child class has a single method abstract, then that child class will be become abstract and hence cannot be instantiated.

Consume non-abstract static method of Abstract class

Following example shows that a public static non-abstract method of an abstract class can be consumed in another class.
using System;
using System.Collections.Generic;

namespace ConsoleAppArray
{
    abstract class AbstractClass
    {
        public static void Show()
        {
            Console.WriteLine("I am in a static method of abstract class");
        }
    }
    public class Program
    {
        static void Main(string[] args)
        {
            AbstractClass.Show();
        }
    }
}

.NET Abstract Class Examples

Assembly class is an abstract class. It represents an assembly, which is a reusable, versionable, and self-describing building block of a common language runtime application.

public abstract class Assembly : System.Reflection.ICustomAttributeProvider, System.Runtime.Serialization.ISerializable
Example of using methods of Assembly class


using System;
using System.Reflection;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string location = Assembly.GetAssembly(typeof(Program)).Location;
            Console.WriteLine(location);
            location = Assembly.GetEntryAssembly().Location;
            Console.WriteLine(location);
            location = Assembly.GetExecutingAssembly().Location;
            Console.WriteLine(location);
        }
    }
}


Edited on 5th July 2023

No comments:

Post a Comment

Hot Topics