Home > Article > Backend Development > What is inheritance in C#? What does C# inheritance mean?
Inheritance is an important concept in C#. Inheritance is a concept that defines parent and subclasses. Subclasses inherit the methods and properties of the parent class. , but at the same time, subclasses can also define their own methods if needed. [Recommended tutorial: C Tutorial]
Now let’s see how to add the concept of inheritance to the code.
Step 1) The first step is to change the code of the Tutorial class, in this step we add the following code to the Tutorial.cs file.
We now need to add the "protected" access modifier to the TutorialID and TutorialName fields. Only with this access modifier can the subclass use the fields of the parent class.
Step 2) The second step is to add our new subclass, the name of this class will be "Guru99Tutorial". In this step, we add the following code to the Tutorial.cs file. The code should be placed after the Tutorial class definition.
Code description:
The first step is to create the Guru99Tutorial subclass. We also need to mention that this class will become a subclass of the Tutorial class.
Next, we define a method called RenameTutorial, which will be used to rename the TutorialName field. This method accepts a string variable containing the new name of the tutorial.
Then, we assign the parameter pNewName to the TutorialName field.
Note: - Even if we don’t define TutorialName field in “Guru99Tutorial” class, we can still access this field. This is because "Guru99Tutorial" is a subclass of Tutorial class. Because we set the fields of the Tutorial class to be protected, they can be accessed through this class.
Step 3) The last step is to modify our Program.cs file. In our console application, we will create an object of Guru99Tutorial class. With this object, we will call RenameTutorial. Then, we will display the TutorialName field with the help of GetTutorial.
{ class Tutorial { protected int TutorialID; protected string TutorialName; public void SetTutorial(int pID,string pName) { TutorialID=pID; TutorialName=pName; } public String GetTutorial() { return TutorialName; } } class Guru99Tutorial:Tutorial { public void RenameTutorial(String pNewName) { TutorialName=pNewName; } static void Main(string[] args) { Guru99Tutorial pTutor=new Guru99Tutorial(); pTutor.RenameTutorial(".Net by Guru99"); Console.WriteLine(pTutor.GetTutorial()); Console.ReadKey(); } } }
The first step is to create an object for the Guru99Tutorial class. This is done via the 'new' keyword. Please note that this time we did not create an object of the Tutorial class.
We use the RenameTutorial method of the Guru99Tutorial class to change the TutorialName field. We pass the string ".Net by Guru99" to RenameTutorial.
Then we call GetTutorial. Please note that even though this method is not defined in the Guru99Tutorial class, we still have access to it. The output of GetTutorial will then be displayed to the console via Console.WriteLine.
If the above code is entered correctly and the program executes successfully, the following output will be displayed.
Output:
From the output, we can clearly see that the TutorialName field has been renamed to ".Net by Guru99". This is accomplished by the RenameTutorial method called by the subclass.
The above is the detailed content of What is inheritance in C#? What does C# inheritance mean?. For more information, please follow other related articles on the PHP Chinese website!