Abstract class example

The base class

An abstract class is an object wich typically acts as an base class for other classes. The abstract class is just a class with the abstract keyword. Advantage of the abstract class is that it's not possible to create instances of it. So there will be no misunderstandings. Abstract classes can have abstract methods. An abstract method is a methods wich has to be overridden in the derived class. An abstract method is ideal for methods from which the code is not available in the base class  but hast to be implemented in the derived class.

    /// 
    /// abstract base class
    /// 
    public abstract class Person
    {
        public decimal Salary{ get; set; }
        
        public abstract string WhatAreYouDoing();

        public string WhatDoYouEarn()
        {
            return string.Format("I earn {0} milion euro's", Salary);
        }
    }

    /// 
    /// derived football player
    /// 
    public class FootbalPlayer : Person
    {
        public override string WhatAreYouDoing()
        {
            return "Playing football";
        }
    }

    /// 
    /// derived footballtrainer
    /// 
    public class FootballTrainer : Person
    {
        public override string WhatAreYouDoing()
        {
            return "Shouting all the time";
        }
    }

So in this example we have a abstract class from wich the other class are deriving. The abstract method WatAreYouDoing has to be implemted in the deriving class otherwise you get a error at compile time. Next to that there is some base logic available to the deriving class: WhatDoYouEarn.

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!