c# – Abstract Classes

When you organise your code into parent and child classes (in order to fully utilize the concept of inheritance in order to cut down code duplication), you may end up with parent classes, that are just there for the purpose of holding base-class code. The class itself isn’t needed for instantiating objects.

These types of class (which you never instantiate objects from) are referred to as “abstract classes”. You never use an abstract class directly to initialize an object.

Here is an example of abstract classes in action:

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

namespace AbstractClass1
{

    // hwer
    public abstract class Employee
    {
        public string Name { get; set; }
        public double Salary { get; set; }
        private DateTime HireDate;

        // The "virtual" key word means that
        // this method can be over-rided
        // by the child class, if required, otherwise it is implicitly
        // inherited
        public virtual void Hire()
        {
            HireDate = DateTime.Now;
        }

        // This is an abstract method.
        // This line forces child classes to include a method with the same name, along with a definition.
        // this is a bit like the same thing that you also find in interfaces.
        public abstract void GiveRaise();

    }

    public class Worker : Employee
    {
        public override void GiveRaise()
        {
            Salary += Salary * 0.03;
        }
    }

    public class Manager : Employee
    {
        public override void GiveRaise()
        {
            Salary += Salary * 0.05;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Worker David = new Worker();
            David.Name = "David Smith";
            David.Salary = 60000.00;
            David.GiveRaise();
            Console.WriteLine("New salary for {0} is {1}",David.Name,David.Salary);

            Manager Goliath = new Manager();
            Goliath.Name = "Goliath Banks";
            Goliath.Salary = 60000.00;
            Goliath.GiveRaise();
            Console.WriteLine("New salary for {0} is {1}", Goliath.Name, Goliath.Salary);

            List ListOfEmployees = new List();

            // this essentially converts this object into an instance of the abstract class.
            // It will still have access to original properties/method as defined in the child class, but only the ones
            // that are also listed in the abstract. It will lose access to everything else, that has been defined in the child class.
            // The same thing happens when using interfaces.
            ListOfEmployees.Add(David);
            ListOfEmployees.Add(Goliath);

            foreach (Employee Person in ListOfEmployees)
            {
                Person.Salary = 100000.00;      // here we reset the salary, and make them the same for each object, to make a better comparison.
                Person.GiveRaise();       // Depending on which class the object was instantiated from, a different method is applied.
                Console.WriteLine("New salary for {0} is {1}", Person.Name, Person.Salary);
            }

        }
    }
}

this gives the following output:

 

 

As you can see, interfaces and abstract classes are quite similar.

 

However the biggest difference between the two is that you can make a class comply with multiple interfaces, using the following syntax:

 
class {classname} : {interface1}, {interface1},……{interfaceX}
 

Note: the more interfaces a class has to comply to, the more methods and properties it needs to comply with all of them.

Whereas a class can only be derived from up to one abstract class, but you can define the abstract class’s method’s tasks which can be inherited….and if necessary over-rided. The abstract class can also force it’s classes to contain a method with a specific name (just like in interfaces) by the use of abstract methods as shown above.