More abstract classes in C#

In the last chapter, we have seen intangible classes. In this chapter, we will expand some examples, and throw in some abstract ways in the same way. Abstract methods are permitted only within intangible sections, their definitions look like a regular method, but they have no code inside them:


abstract class FourLeggedAnimal
{
    public abstract string Describe();
}

So, you do not want to define an empty method that does nothing? Since an abstract method is the responsibility of implementing the same method in all the sub-classes, in fact, to ensure that this method is defined in your sub-classes, time has been examined in compiling. Once again, this is a great way to create base squares for some, while still subclass should be able to do. Keeping this in mind, you can always treat a sub-class as your subclass, whenever you have to use methods defined as abstract methods on the base class. For example, consider the following example:


namespace AbstractClasses
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Collections.ArrayList animalList = new System.Collections.ArrayList();
            animalList.Add(new Dog());
            animalList.Add(new Cat());
            foreach(FourLeggedAnimal animal in animalList)
                Console.WriteLine(animal.Describe());
            Console.ReadKey();
        }
    }

    abstract class FourLeggedAnimal
    {
        public abstract string Describe();
    }


    class Dog : FourLeggedAnimal
    {

        public override string Describe()
        {
            return "I'm a dog!";
        }
    }

    class Cat : FourLeggedAnimal
    {
        public override string Describe()
        {
            return "I'm a cat!";
        }
    }
}

As you can see, we make an Arielist to incorporate our animals. We again mean a new dog and a new cat and add them to the list. They are presented as a dog and a cat respectively, but they also type FourLeggedAnimal, and because the compiler knows that the sub-sections of that class include the Describe() method, in fact that method Is allowed to call, without knowing the exact type of animal, by typecasting of four types of animations, which we do in foreign currency loop, in these scenarios, sub-classes In order to gain access to the members we are very useful.