CSharp - Abstract class with concrete methods.

Introduction

Abstract class can have concrete methods.

The child class may or may not override those methods.

Demo

using System;

abstract class MyAbstractClass
{
    protected int myInt = 25;
    public abstract void ShowMe();
    public virtual void CompleteMethod1()
    {//  w ww  .j av  a 2 s.c  o  m
        Console.WriteLine("MyAbstractClass.CompleteMethod1()");
    }
    public void CompleteMethod2()
    {
        Console.WriteLine("MyAbstractClass.CompleteMethod2()");
    }

}
class MyConcreteClass : MyAbstractClass
{
    public override void ShowMe()
    {
        Console.WriteLine("I am from a concrete class.");
        Console.WriteLine("My ShowMe() method body is complete.");
        Console.WriteLine("value of myInt is {0}", myInt);
    }
    public override void CompleteMethod1()
    {
        Console.WriteLine("MyConcreteClass.CompleteMethod1()");
    }
}
class Program
{
    static void Main(string[] args)
    {
        //Error:Cannot create an instance of the abstract class
        // MyAbstractClass abstractOb=new MyAbstractClass();
        MyConcreteClass concreteOb = new MyConcreteClass();
        concreteOb.ShowMe();
        concreteOb.CompleteMethod1();
        concreteOb.CompleteMethod2();
        Console.WriteLine("\n\n*** Invoking methods through parent class reference now***\n");
        MyAbstractClass absRef = concreteOb;
        absRef.ShowMe();
        absRef.CompleteMethod1();
        absRef.CompleteMethod2();
    }
}

Result

Note

We can use the abstract class reference to point to the child class objects, and then invoke the associated methods.

An abstract class can contain fields.

Whenever your class has at least one abstract method, the class is an abstract class.

Related Topic