Hi everyone...I have this

abstract class Program
        static void Main(string[] args)
        public abstract void m1();

   class Program2 : Program
       public override void m1()


I inherit Program and implement the method m1(), can anyone explain in a simple way why I would not just implement this method in Program instead of Program2. Surely this would save time in creating a child class and overriding the method? I know there must be a reason behind it!

Is the main reason for an abstract class to have reusable code available to subclasses and save time?


You use abstract method to indicate that the child classes all have a behavior that is implemented in different ways.

For example, we could have an abstract class 'Animal' with child classes 'Lion', 'Giraffe' and 'Dung Beetle'. The 'Animal' class has an abstract method 'Eat'. For a Lion object, this would mean 'hunt other animal', for a Giraffe object 'find tree', and a Dung Beetle would 'seek dung'.

Now that we have these classes, we can create an array of Animals, and assign different animals to this array:

Animal[] jungleLife = new Animal[3];

jungleLife[0] = new Lion();
jungleLife[1] = new Giraffe();
jungleLife[2] = new DungBettle();

Now if we want all these animals to eat, we know that they all implement the abstract method (or we couldn't create an object of that class). Because they can all be asigned to the animal array, we don't have to worry about what type they actually are when we want them to eat:

foreach (Animal a in jungleLife) {

Due to the nature of abstract methods the appropriate method for each animal will be called.

Just one more thing...you have no choice whether to make the animals eat as eat() is an abstract method and it needs to be overridden else it will cause a compile error?

Am I right?

You can't even create an object of type Animal, as it is abstract.