Lesson 19 Teaching the Concept of Inheritance In Class #C

This is lesson no 19 and today i will teach you about Concept of Inheritance In Class #C. The most important aspect of object-oriented programming languages (OOP) Theorem #C is based on the concept of an inheritance. An inheritance means that new classes are created that have inherited some of their properties from the parent class Parent Class.

Concept of Inheritance In Class #C

The whole .NET framework is not based on the concept of inheritance, which is the famous phrase “everything is an object”, from These are the results. In C #, even a simple number is an example of a class that is itself a mother class object.

System is inherited. Although .NET Framework, it also allows you to Give your desired value directly to a number, without the need for a new version of the class Create an integer.

It may be a little difficult to explain the concept of inheritance, but practical examples help a lot. Sample
The following code is a simple example of the concept of inheritance in C #:

public class Animal
public void Greet()
Console.WriteLine(“Hello, I’m some sort of animal!”);
public class Dog : Animal

In the above code, in the first step, we have defined a class called Class Animal and it has a simple method for printing The message is welcome. Then create a Class Dog class and insert one

: In contrast, to #C

We have declared that this class should be inherited from the Animal class. The interesting thing about the above example is that The relationship between dog and animal is the same in nature, meaning that Dog is a subset of animals.

Now Let’s look at how to use the classes:

Animal animal = new Animal();
Dog dog = new Dog();

If you run the example code above, you will see that even though the Greeting () method is defined for the Dog class We did not, but this class can welcome the message message because it uses the Greeting () method from its parent class.

That is, Class Animal is inherited. Of course, the initial welcome message of the above code is a bit incomprehensible. Let’s add code to the previous example to find out What an animal welcomes us!
Animal animal = new Animal ();
animal.Greet ();
Dog dog = new Dog ();
dog.Greet ();

Next to the method added to the Class Dog class, you will see two important things. We have a virtual keyword We have added the Virtual keyword to the Class Animal class method, on the other hand the averride keyword We have also used it for Class Dog class.

In C #, you can not rewrite or override a member of a class unless Set the title Virtual. If you want, you can use the inherited method using the word Call the base key as your primary. In the form of the following example code:

public override void Greet ()
base.Greet ();
Console.WriteLine (“Yes I am – a dog!”);

Methods are not the only ones that can be inherited in classes, other characteristics of a class such as Fields (Fileds) and Properties are also inherited. You only need to see the rules of the field.

Note the visibility we described in the previous lesson.

Inhertance is not just about one class after another. You can get a hierarchy of Make regular classes. For example, you can create a class called Puppy that is from The Dog class is inherited, while the Dog is the child of the Animal class.

Important Note: In C #, you cannot inherit a class from several different classes. To inherit this work
Inheritance multiple is said not to be supported in C #.

%d bloggers like this: