Lesson 21 Complete Study of Class Abstract base classes in #C

Today is lesson number 21, and I’ll demonstrate you how to learn all of the Class Abstract base classes in #C. We studied about the notion of Class Abstract base classes in C# in the previous session. In this lesson, we’ll expand on the previous lesson’s example by using the fixed methods of Abstract Methods.

Class Abstract base classes in #C

Code Fixed classes have the same definition as ordinary methods, but no code is written inside them:

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

But why would we want to define an empty method if it isn’t going to help us? Because the Basic Method is a requirement or intent for all child classes, Run the currently selected class.

In fact, by doing so, we may determine whether all of the Children classes have declared this function at the moment of programme execution.

While we may regulate what the subclass should be able to accomplish, this method is the best way to determine a base class for something in the application.

Keeping this in mind, you can always make a child a subclass, such as Base. Consider the following code as an 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
{
12
public override string Describe()
{
return “I’m a dog!”;
}
}
class Cat : FourLeggedAnimal
{
public override string Describe()
{
return “I’m a cat!”;
}
}
}

We’ve constructed an ArrayList array list with the names of various correct animals, as you can see in the example code.

Then we made a new Dog and Cat example and put it to the list. The sample is made up of the Dog and Cat classes, as well as a FourLeggedAnimal class, in the object described.

The method can be used as long as the compiler recognises that the child class is derived from the base class. Without knowing what kind of animal the object is, call and execute Describe ().

So, in the loop foreach loop for the class We did FourLeggedAnimal, we performed a data type conversion (TypeCasting), accessing members of the subclass child class. Many people will find it useful.

%d bloggers like this: