Inheritance in C#

C# Inheritance in one of the key properties of object oriented programming. In this article, learn what inheritance is and how to implement inheritance in C#.

C# Inheritance in one of the key properties of object oriented programming. Inheritance is a relationship that defines one entity in terms of another.
 
C# language support inheritance by inheriting new classes from exisitn classes. Class inheritance defines a new class in terms of one parent class or one or more interfaces. The new class inherits its interface and implementation from its parent class and method signatures. The new class is called a subclass or a derived class.
 
Class inheritance combines interface inheritance and implementation inheritance. Interface inheritance defines a new interface in terms of one or more existing interfaces. Implementation inheritance defines a new implementation in terms of one or more existing implementations.
 
Classes in C# support only single inheritance, and Object is the ultimate base class for all classes. C# language supports multiple inheritance via interfaces. Learn more here on How to implement multiple inheritance in C#.
 
Access modifiers in a classes members give different levels of access to derived classes. Below is a table that describes the access a child class has to the members of the inherited class depending upon the access modifier.
 
The following six accessibility levels can be specified using the access modifiers: 
  • public: Access is not restricted. Any caller program can have access to a public member.
  • protected: Access is limited to the containing class or types derived from the containing class.
  • internal: Access is limited to the current assembly.
  • protected internal: Access is limited to the current assembly or types derived from the containing class.
  • private: Access is limited to the containing type.
  • private protected: Access is limited to the containing class or types derived from the containing class within the current assembly.
C# only makes use of public inheritance, meaning that you cannot specify an inheritance modifier in C#.
 
The following code example shows an example of a class deriving inheritance with public access specified.
 
Inheritance Example 1
  1. using System;  
  2. class A // derived from object behind the scenes  
  3. {  
  4. public void F() { Console.WriteLine("A.F"); }  
  5. }  
  6. class B : A // B inherits A  
  7. {  
  8. public void G() { Console.WriteLine("B.G"); }  
  9. }  
  10. class Test  
  11. {  
  12. static void Main()  
  13. {  
  14. B b = new B();  
  15. b.F(); // Inherited from A  
  16. b.G(); // Introduced in B  
  17. A a = b; // Treat a B as an A, polymorphic access  
  18. a.F();  
  19. Console.ReadLine();  
  20. }  
  21. }  
Inheritance Example 2
 
The following code example shows an example of a class deriving inheritance with protected access specified.  
  1. // example1 protected  
  2. class A  
  3. {  
  4. protected int x = 123;  
  5. }  
  6. class B : A  
  7. {  
  8. void F()  
  9. {  
  10. A a = new A();  
  11. B b = new B();  
  12. a.x = 10; // Error  
  13. b.x = 10; // OK  
  14. }  
  15. }  
The following code example shows another example of private inheritance that enables direct access to protected members.
  1. // example2 protected  
  2. using System;  
  3. class MyClass  
  4. {  
  5. protected int x;  
  6. protected int y;  
  7. }  
  8. class MyDerivedC : MyClass //private inheritance  
  9. {  
  10. public static void Main()  
  11. {  
  12. MyDerivedC mC = new MyDerivedC();  
  13. // Direct access to protected members:  
  14. mC.x = 10;  
  15. mC.y = 15;  
  16. Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y);  
  17. Console.ReadLine();  
  18. }  
  19. }  
The output looks like the following:

fig-5.12.gif
 
Conclusion
 
Hope this article would have helped you in Inheritance in C#. See other articles on the website on .NET and C#.
 
Further recommended readings on C# inheritance 
 
Here are more articles on inheritance and object oriented programming in C#.
 
Types of Inheritance in C# 
In this article, we will learn about C# inheritance and types of inheritance in C# and .NET with code examples.

Overview of Inheritance in C#
In this article, you will learn about inheritance in C#. Inheritance is one of the major pillars of object-oriented programming. Here is an overview of inheritance.

Inheritance with Example in C#
Here are some example of inheritance using C#.
 
Learn all about Inheritance in C#
In this article, you will learn about Inheritance in C#.
 
Playing with inheritance in C#
This article explains the various behaviors of inheritance in various cases.
 
Inheritance examples in C#
This article explains inheritance with examples in C#.
 
Multiple Inheritance in C#
Can you inherit from multiple classes in C#? Simply put, this cannot be done. However there are ways around it. From a design perspective you must ask yourself, will a Class fully represent an object?
 
Multiple Inheritance in C# using Interfaces
This is the simple mathematical operation program demonstrating how multiple inheritance can be achieved in C# using Interface Concept.
 
Inheritance in C#
Interface inheritance defines a new interface in terms of one or more existing interfaces. Implementation inheritance defines a new implementation in terms of one or more existing implementations.