Home Backend Development C#.Net Tutorial What is inheritance in C#? What does C# inheritance mean?

What is inheritance in C#? What does C# inheritance mean?

Jan 15, 2019 pm 02:47 PM
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.

What is inheritance in C#? What does C# inheritance mean?

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.

What is inheritance in C#? What does C# inheritance mean?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(); 
  }
 }
}
Copy after login

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:

What is inheritance in C#? What does C# inheritance mean?

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does the shift key mean? What is the use of the shift key? What does the shift key mean? What is the use of the shift key? Feb 22, 2024 pm 05:28 PM

The Chinese name of the Shift key is the shift key. Analysis: The Chinese name of the 1Shift key is the shift key. It can be used with many keys, so it has many functions. Use the 2shift key to switch input methods, and hold down the Shift key to select multiple files continuously. 3. You can cancel the automatic startup of the software at boot. If you keep holding down the Shift key when turning on the computer, all the startup items that have been set will be cancelled. 4. Select the file you want to delete, then press and hold the Shift key, and then press the Delete key. Then you can delete the file directly. The file will not enter the Recycle Bin. Supplement: What does shortcut key mean? 1 Shortcut key refers to completing an operation through certain specific keys, key sequence or key combination. Many shortcut keys are often related to Ct

What does demo mean? What does demo mean? Feb 12, 2024 pm 09:12 PM

The word demo is no longer unfamiliar to friends who like to sing, but many users who have never been exposed to it are curious about what demo means. Now let’s take a look at the meaning of the demo brought by the editor. What does demo mean? Answer: Demo tape. 1. The pronunciation of demo is ['deməʊ] in English and ['demoʊ] in America. 2. Demo is the abbreviation of "demonstration", which generally refers to the preliminary effect of listening to a song before it is officially recorded. 3. Demo is used as a noun to refer to sample tapes and sample records. The meaning of verb is trial (especially software), demonstration and demonstration;

What is the analysis of 2.8k screen? What is the analysis of 2.8k screen? Jan 02, 2024 pm 12:21 PM

We often see the introduction of how many K screens we have when buying TVs, computers or mobile phones, such as 2.8K screens. At this time, there will be friends who don’t know much about electronic devices and will be curious about what this 2.8K screen means and what the resolution is. What does 2.8k screen mean? Answer: 2.8k screen means that the screen resolution is 2880*18002K, which means the number of horizontal pixels is greater than 2000. For the same size screen, the higher the resolution, the better the picture quality. Introduction to resolution 1. Since the points, lines and surfaces on the screen are all composed of pixels, the more pixels the monitor can display, the finer the picture, and the more information can be displayed in the same screen area. 2. The higher the resolution, the greater the number of pixels, and the sharper the sensed image.

What does game bug mean? What does game bug mean? Feb 18, 2024 am 11:30 AM

What do game bugs mean? During the process of playing games, we often encounter some unexpected errors or problems, such as characters getting stuck, tasks being unable to continue, screen flickering, etc. These abnormal phenomena are called game bugs, that is, faults or errors in the game. In this article, we'll explore what game bugs mean and the impact they have on players and developers. Game bugs refer to errors that occur during the development or operation of the game, causing the game to fail to run normally or to behave unexpectedly. These errors may be due to

Analyze the meaning of computer system pe Analyze the meaning of computer system pe Dec 30, 2023 pm 02:41 PM

If we want to install a Windows system, we will definitely come into contact with the pe system, but many friends do not understand what the computer system pe means. In fact, it is a pre-installation environment system provided by Windows for users. What does computer system pe mean: Answer: Computer system pe is the windows pre-installation environment, which is the abbreviation of English PreinstallEnvironment. It is mainly used for Windows system installation and maintenance. 1. PE is a subsystem based on win32. It can be started directly from a USB disk or CD when we have not installed it. 2. For ordinary users, the most commonly used tasks in PE are to install Windows systems, repair systems, and maintain disk environments. 3. This

What does printer error code 0x0000011b mean? What does printer error code 0x0000011b mean? Feb 18, 2024 pm 10:02 PM

What does 0x0000011b mean when connecting to a printer? Users often encounter various error codes when using computers, laptops or other devices. Among them, 0x0000011b is a common printer connection error code. So, what does connecting printer 0x0000011b mean? First, we need to understand the basic principles of printer connection. When we need to print files from the computer, we usually need to connect the printer to the computer for data transfer between the two. This connection can be made via

Detailed introduction to what ENABLED means Detailed introduction to what ENABLED means Jul 07, 2023 pm 08:17 PM

enabled is feasible. enabled means: can be activated. Enabled; disabled means: defective. closure. There is a bios setting on the computer. These two words refer to failure and feasibility. Let us explain in detail what enabled means. let us see. 1. "Disabled" can also mean unavailable, disabled, or the project is closed after setting. 2. "Enabled" means that it can be used, turned on and enabled. 3. Therefore, when we set the bios, we should set a function to enabled, and to disable a function, we only need to set it to disabled. Of course, pay attention to saving after setting, usually F10 to save. The above is what ENABLED is

In C language, what does Realloc mean? In C language, what does Realloc mean? Aug 28, 2023 pm 12:41 PM

The C library's memory allocation function void*realloc(void*ptr,size_tsize) attempts to resize the memory block pointed to by ptr that was previously allocated using a malloc or calloc call. Memory Allocation Functions Memory can be allocated in two ways: Once memory is allocated at compile time, it cannot be changed during execution. Either there is insufficient memory or it is a waste of memory. The solution is to create memory dynamically, i.e. based on the program's needs during execution. The standard library functions used for dynamic memory management are as follows: malloc()calloc()realloc()free()realloc() function is used to reallocate allocated memory. Allocation can be reduced or increased

See all articles