In the Multilevel inheritance, a derived class will inherit a base class and as well as the derived class also act as the base class to other class. For example, three classes called A, B, and C, as shown in the below image, where class C is derived from class B and class B, is derived from class A. In this situation, each derived class inherit all the characteristics of its base classes. So class C inherits all the features of class A and B.
Example: Here, the derived class Rectangle is used as a base class to create the derived class called ColorRectangle. Due to inheritance the ColorRectangle inherit all the characteristics of Rectangle and Shape and add an extra field called rcolor, which contains the color of the rectangle.
This example also covers the concept of constructors in a derived class. As we know that a subclass inherits all the members (fields, methods) from its superclass but constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. As shown in the below example, base refers to a constructor in the closest base class. The base in ColorRectangle calls the constructor in Rectangle and the base in Rectangle class the constructor in Shape.
Details of r1: Rectangle is Fibonacci rectangle Width and Length are 2 and 3.236 Color is pink Area is 6.472 Details of r2: Rectangle is Square Width and Length are 4 and 4 Color is black Area is 16
- C# | Inheritance
- C# | Inheritance in Constructors
- C# | Inheritance in interfaces
- C# | Multiple inheritance using interfaces
- Late Binding using Reflection in C#
- Difference between Method Overriding and Method Hiding in C#
- Method Hiding in C#
- C# | Restrictions on Properties
- Object and Collection Initializer in C#
- Anonymous Method in C#
- Different ways to create an Object in C#
- Shallow Copy and Deep Copy in C#
- Destructors in C#
- Invoking an overloaded constructor using this keyword in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.