Lesson 20 Teaching the Concept of Class Abstract Class in #C

This is our lesson number 20, and today I’ll teach you how to teach the concept of Class abstract class in #C. Base or absolute classes In a tree hierarchy of classes Abstract, Basic, and Mother are signified by the abstract keyword. To put it another way, these classes are Is the reference classes, and the rest of the classes are inherited from it.

Class Abstract Class in #C

Note: The crucial point to remember about Class Abstract fixed classes is that you can’t acquire a sample or object from Build on them; doing so would result in a compiler error.

You can create a child or subclass instead of building an object from basic classes, as seen in We built from the base class and then generated our objects from the child class in the previous session.

But when might you need basic classes? If we want to be honest with each other, you can Write a complete project, without the need for a basic class. But these kinds of classes It is very suitable for a specific purpose and it is used in framework or FrameWork.

To This is why there are so many fundamental classes in the.NET Framework. We build a base class named Animal Legged Four in the code below, and then another class called Dog is inherited from the base class.

namespace AbstractClasses
{
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog();
Console.WriteLine(dog.Describe());
Console.ReadKey();
}
}
abstract class FourLeggedAnimal
{
public virtual string Describe()
13
{
return “Not much is known about this four legged animal!”;
}
}
class Dog : FourLeggedAnimal
{
}
}

If you compare the code of the above example with the examples of the previous lesson, teaching the concept of inheritance in C #, there is a big difference between You will not see them. In fact, the abstract keyword in front of the FourLeggedAnimal class name is important
The biggest difference is in these examples.

As you can see, we created an example of a Dog class and then We called the inherited method () Describe from the FourLeggedAnimal class. Now let’s one Create an example of the FourLeggedAnimal class, as follows:

FourLeggedAnimal someAnimal = new FourLeggedAnimal();

But you will encounter a compiler error. The error description is as follows:

Cannot create an instance of the abstract class or interface

‘AbstractClasses.FourLeggedAnimal’

As you can see in the previous exbample, we called the inherited method (Describe), ut in fact the application It didn’t have much for us. So let’s rewrite or override it with the following code:

class Dog : FourLeggedAnimal
13
{
public override string Describe()
{
return “This four legged animal is a Dog!”;
}
}
In the example code above, we completely rewrote or override the method. But sometimes you may want behavior Inherit from the base class and add the code value to it. For this purpose, you must use the base keyword Used, which refers to the class we inherited. As the following code:

abstract class FourLeggedAnimal
{
public virtual string Describe()
{
return “This animal has four legs.”;
}
}
class Dog : FourLeggedAnimal
{
public override string Describe()
{
string result = base.Describe();
result += ” In fact, it’s a dog!”;
11
return result;
}
}

You can now make another Subclass child tide for the Fourleggyanimal Card. In the next lesson, gives a more advanced example and how to work with basic methods of methods abstract) (tutorials We will give.

%d bloggers like this: