Home >Backend Development >C#.Net Tutorial >Inheritance in C#
Inheritance in C# is the process of acquiring all the properties of one class into another class. There are two classes referred to as base class and derived class. The base class is also known as a parent class, and the properties or methods of this class we want to inherit to another class.
The derived class is known as the child class that is used to inherit the properties and methods of the base class or parent class. It helps in reusing the same code again, and no need to define the same properties again and again.
Inheritance is one of the powerful concepts or fundamental attributes of object-oriented programming language. It is widely used in all the OOPs based programming language. Its main purpose is to use the same code again. The example of the Basic Structure of Inheritance is given below:
class BaseClass { } class ChildClass: BaseClass {}
There are different types of Inheritance in C#:
In Single inheritance, there is only one base class and one derived class. It means the child class will inherit the properties of the parent class and use them.
Example:
class BaseClass { public void hello() { Console.WriteLine("Parent's Hello Method"); } } class ChildClass : BaseClass { public void World() { Console.WriteLine("Child's World Method"); } }
In this type of inheritance, there is only one base class, and multiple derived class are available. If a class is created by using another derived class is known as multilevel inheritance.
Example:
class BaseClass { public void hello() { Console.WriteLine("Parent's Hello Method"); } } class ChildClass : BaseClass { public void World() { Console.WriteLine("Child's World Method"); } } class SecondChildClass : ChildClass { public void Hi() { } }
In this type of inheritance, this can be achieved with the help of multiple interfaces, not with a class. In simple words, C# does not support multiple inheritances, but if you want to achieve it, then it can be achieved with the help of interfaces only.
Example:
Interface A {} Interface B {} Class C: A, B {}
In this type of inheritance, there is one parent class, and the other derived classes inherit the same parent class to achieve this inheritance.
Example:
class BaseClass { public void hello() { Console.WriteLine("Parent's Hello Method"); } } class ChildClass : BaseClass { public void World() { Console.WriteLine("Child's World Method"); } } class SecondChildClass : BaseClass { public void Hi() { } }
Below are the advantages of Inheritance given.
Following are the features of Inheritance described.
Inheritance is used when the same code needs to be used in another class. So, instead of writing the same code, again and again, there is a concept of object-oriented programming that is Inheritance that helps in using the same functionality like methods or properties of one class to another class. With the help of semicolon (:), we can inherit the members of the base class to child class or derived class.
It makes it easy to work on because it helps in avoiding the confusion of method calling from which class method is getting called. It makes the code reusability and makes the file lighter in weight with less number of lines of source code. This makes the code less redundant and more flexible to use in different classes. It keeps the structure of the program that helps in reading the code easily.
The above is the detailed content of Inheritance in C#. For more information, please follow other related articles on the PHP Chinese website!