KodeBLOG

Kode Blog - Inspiring And Empowering Developers.

Home About Us Courses Categories Blog Contact Us

C# Abstract Class

Introduction

An abstract class is a class that cannot be initialized on its own. Abstract class are usually partially implemented or not implemented at all.

Topics to be covered

  • Benefits of abstract classes?
  • How to define an abstract class in C#
  • Abstract subclass (derived class) and overriding
  • Rules of abstract classes

Benefits of Abstract Classes

Abstract classes allow you to group related functionality and properties together. All of the classes derived from the abstract class will be forced to implement all methods and properties that are defined in the abstract class. Let's suppose you are developing an application that should support multiple database engines without rewriting the application code. You can create an abstract class that defines properties such as;

  • server name/ip address
  • database name
  • database port
  • database user id
  • database password

You can also be interested in methods such as;

  • connectToDatabase
  • closeDatabaseConnection
  • executeSQLStatement
  • selectQueryResults

The above properties and methods are common to all classes that interact with databases. Defining an abstract class for the above scenario ensures that all classes derived from the abstract class will have the above properties and methods

How to define an abstract class in C#

An abstract class is defined using the abstract keyword. The following code defines an abstract class with a single method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpAbstractClass
{
    abstract class AbstractClassSyntax
    {
        protected int some_number;
        public abstract int someNumber
        {
            get;
            set;
        }
        public abstract string method(string arg1, string arg2);
    }
}

HERE,

  • abstract class AbstractClassSyntax uses the abstract keyword to defined an abstract class AbstractClassSyntax
  • public abstract string method(string arg1, string arg2); uses the abstract keyword to define an abstract method named method that accepts two arguments string arg1, string arg2

Now that we have defined an abstract class, let us look at how we can create a derived class from the abstract class.

Abstract subclass (derived class) and overriding

A derived class is a child class of the abstract class. The following code show how to create a derived class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpAbstractClass
{
    class DerivedClass : AbstractClassSyntax
    {
        public override int someNumber
        {
            get { return some_number; }
            set { some_number = value; }
        }

        public override string method(string arg1, string arg2)
        {
            return arg1 + " " + arg2;
        }
    }
}

HERE,

  • class DerivedClass : AbstractClassSyntax defines a class DerivedClass that is a child of AbstractClassSyntax
  • public override int someNumber{...} implements the abstract property someNumber
  • public override string method(string arg1, string arg2) implements the abstract method method defined in the abstract class AbstractClassSyntax
  • The keyword override is used to alter the implementation of the property/method that is defined in the abstract class AbstractClassSyntax.

Rules of abstract classes

  • An abstract class cannot be a sealed class
  • An abstract properties and methods must be public
  • An abstract class cannot be static

Similarities between an abstract class and an interface

  • Both define methods that all classes that inherit them must implement.
  • Both cannot be sealed.

Differences between an abstract class and an interface

  • An abstract class define properties while an interface cannot
  • An abstract class can provide [partial] implementation of properties and methods while an interface cannot
  • Child classes of an abstract class can only inherit from a single parent while classes that inherit from an interface can inherit from multiple interfaces

Summary

Abstract classes are useful for defining common properties and methods that child classes must override and provide their own implementation.

Tutorial History

Tutorial version 1: Date Published 2015-08-21