Introduction:

In this article I will introduce to you what is abstract class in C# or how to use abstract class in c# or inherit an abstract class or override abstract class method with example:

Description:

An abstract class is a particular kind of class that cannot be instantiated. it only allows other classes to inherit from it but cannot be instantiated. The purpose of an abstract class is to provide a common methods or properties of a base class that multiple derived classes can use. the Derived classes of the abstract class must implement all abstract methods. When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method.

For Example:

  Override the virtual method with an abstract method:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication1
{
    // compile with: /target:library
    public class BaseClass
    {
        public virtual void GetProductByName(string ProductName)
        {
            Console.WriteLine(String.Format("My ProductName is {0} .", ProductName));
        }
    }
 
    public abstract class CommonClass : BaseClass
    {
        public abstract override void GetProductByName(string ProductName);
    }
 
    public class CustomerClass : CommonClass
    {
        public override void GetProductByName(string ProductName)
        {
            Console.WriteLine(String.Format("My Product Name is {0} .", ProductName));
           
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            CustomerClass firstProduct = new CustomerClass();
            firstProduct.GetProductByName("Samsung E732Z");           
            Console.ReadLine();
        }
 
    }
}

 

 

Above example we explained how to override a virtual method in Abstract class and override derived class to Abstract class override method.

Implement Derived classes of the abstract class methods:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication1
{  
    public abstract class CommonClass 
    {
        public abstract void GetProductByName(string ProductName);
    }
    public class CustomerClass : CommonClass
    {
        public override void GetProductByName(string ProductName)
        {
      
            Console.WriteLine(String.Format("My Product Name is {0} .", ProductName));           
      
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            CustomerClass firstProduct = new CustomerClass();
            firstProduct.GetProductByName("Samsung E732Z");           
            Console.ReadLine();
        }
 
    }
}

Above example we explained how to override  Abstract class method by derived class.

I hope this article help to you.

comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like