Obtaining the properties of parents by their children is known as inheritance.
Similarly in Object-oriented programming language, the same applies to establish parent and child relationship between classes so that members defined in one class can be consumed by another class.
Syntax:
Class Base:
{}
Class Child: Base
{}
Example:
using System;
namespace SampleProgram {
class Parent {
public Parent() {
Console.WriteLine(“Parent Constructor”);
}
public void TestParent1() {
Console.WriteLine(“Parent Test 1 Method”);
}
public void TestParent2() {
Console.WriteLine(“Parent Test 2 Method”);
}
}
class Child: Parent {
public Child() {
Console.WriteLine(“Child Constructor”);
}
public void TestChild1() {
Console.WriteLine(“Child Test 1 Method”);
}
public void TestChild2() {
Console.WriteLine(“Child Test 2 Method”);
}
public static void Main(string[] args) {
Child objChild = new Child();
objChild.TestParent1();
objChild.TestParent2();
objChild.TestChild1();
objChild.TestChild2();
}
}
}
The output will be:
Parent Constructor
Child Constructor
Parent Test 1 Method
Parent Test 2 Method
Child Test 1 Method
Child Test 2 Method
C# supports single class inheritance only.
It means a class can inherit from only one base class at a time. As per the below Syntax, the child class Child is trying to inherit from Class Base1 and Base2 but this is not allowed and it throws an error. So the class cannot inherit multiple classes at the same time.
Syntax:
public class Base1
{
}
public class Base2
{
}
public class Child: Base1, Base2
{
}
Multi-Level Inheritance:
As said above multiple inheritance is not possible but C# allows Multi-Level inheritance.
As per the below Syntax, it means the class Child is derived from Class Base and Class SuperChild is
derived from Class Child. So the class SuperChild can access all the members in the Class Base
and Class Child.
Syntax:
public class Base
{
}
public class Child: Base
{
}
public class SuperChild : Child
{
}
Example:
using System;
namespace SampleProgram {
class Parent {
public Parent() {
Console.WriteLine(“Parent Constructor”)
}
public void TestParent1() {
Console.WriteLine(“Parent Test 1 Method”);
}
public void TestParent2() {
Console.WriteLine(“Parent Test 2 Method”);
}
}
class Child: Parent {
public Child() {
Console.WriteLine(“Child Constructor”);
}
public void TestChild1() {
Console.WriteLine(“Child Test 1 Method”);
}
public void TestChild2() {
Console.WriteLine(“Child Test 2 Method”);
}
}
class GrandChild: Parent {
public GrandChild() {
Console.WriteLine(“Grand Child Constructor”);
}
public void TestGrandChild1() {
Console.WriteLine(“Grand Child Test 1 Method”);
}
public void TestGrandChild2() {
Console.WriteLine(“Grand Child Test 2 Method”);
}
public static void Main(string[] args) {
GrandChild objGrandChild = new GrandChild();
objGrandChild.TestParent1();
objGrandChild.TestParent2();
objGrandChild.TestChild1();
objGrandChild.TestChild2();
objGrandChild.TestGrandChild1();
objGrandChild.TestGrandChild2();
}
}
}
}
}
The output will be:
Parent Constructor
Child Constructor
Grand Child Constructor
Parent Test 1 Method
Parent Test 2 Method
Child Test 1 Method
Child Test 2 Method
Grand Child Test 1 Method
Grand Child Test 2 Method