C# Class Inheritance

Inheritance is a very important feature in C#. It can allow you to achieve the polymorphisn, which is very very important feature which could help you to write the code in a good design (easy to extend).

Category

  • Inherit a class
    • Inherit a abstract class
      • Must implement the abstract methods in the abstract class
      • Can override/new the virtual methods in the abstract class
    • Inherit a normal class
      • Can override/new the virtual methods in the abstract class
  • Inherit an interface
    • Must implement all members (properties, methods, events) in the interface.

The different field

When class Student inherits class Person publicly. Those functions and members which are public and protected in class Person can be used in class Student. Functions and members which are private in class Person cannot be used in class Student.

However, if there is a public/protected function in class Person, which uses the private members of class Person. When class Student call that functions, those private members in class Person could also be used via this function.

Base function

Use base.function() in the class Student can call the functions implemented in class Person (the functions should be public/protected). This is often used when class Student rewrite the methods of class Person but still need to call the method of class Person.

About override/new keyword

Polymorphisn often happens when you declare a class Person reference, but you point it to an object instantiated from class Student.

When there is a abstract/virtual method in class Person, class Student could use override/new to rewrite that method. At this moment, call the which are rewrote can lead to charming results.

Since the abstract class can’t be instantiated, we just talked about those virtual - override/new relationship.

  • If the reference type and the object type is the same. Call those functions are calling the function in their own field.
  • If the reference type is the base class and the object type is the subclass.
    • If the method is declared override in subclass. Call this method from the reference is executing the method in subclass.
    • If the method is declared new in subclass. Call this method from the reference is executing the method in base class.

If the method is declared abstract in base class. You cannot call it except the subclass override this method.

[InheritNormalClass] [CSharp]view raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;

namespace testInherit
{
class Person
{
protected int age = 10;
protected int id = 0;
public Person()
{
Console.WriteLine("Person Created!");
}
public void ShowBaseInfo()
{
Console.WriteLine(age.ToString() + ","+ id.ToString());
}

public virtual void ShowType()
{
Console.WriteLine("This is a person");
}

public virtual void ShowName()
{
Console.WriteLine("Person name");
}
}

class Student : Person
{
new int age = 20;
public Student()
{
Console.WriteLine("Student Created!");
}
public void ShowInfo()
{
Console.WriteLine(age.ToString() + ","+ id.ToString());
}

public override void ShowType()
{
Console.WriteLine("This is a student");
}

public new void ShowName()
{
Console.WriteLine("Person name");
}
}

class Program
{
static void Main(string[] args)
{
Student student = new Student();

Console.WriteLine("--------------------");
student.ShowBaseInfo();
student.ShowInfo();
Console.WriteLine("--------------------");
student.ShowType();
Console.WriteLine("--------------------");
Console.WriteLine("--------------------");

Person person = new Person();
Person person_student = student;
Console.WriteLine("--------------------");
person.ShowType();
person_student.ShowType();
student.ShowType();
Console.WriteLine("--------------------");
person.ShowName();
person_student.ShowName();
student.ShowName();
}
}
}
[InheritNormalClass] [CSharp]view raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;

namespace TestCSharp
{
public class Father
{
public static void DoStaticWork()
{
Console.WriteLine("Father.DoStaticWork");
}

public void DoWork()
{
Console.WriteLine("Father.DoWork");
}

public virtual void DoVirtualWork()
{
Console.WriteLine("Father.DoVirtualWork");
}

public virtual void DoVirtualWork2()
{
Console.WriteLine("Father.DoVirtualWork2");
}

public virtual void DoVirtualWorkAll()
{
Console.WriteLine("Father.DoVirtualWorkAll");
}
}

public class Son : Father
{
public static void DoStaticWork()
{
Console.WriteLine("Son.DoStaticWork");
}
public void DoWork()
{
Console.WriteLine("Son.DoWork");
}

public new virtual void DoVirtualWork()
{
Console.WriteLine("Son.DoVirtualWork");
}

public override void DoVirtualWork2()
{
Console.WriteLine("Son.DoVirtualWork2");
}
public override void DoVirtualWorkAll()
{
base.DoVirtualWorkAll();
Console.WriteLine("Son.DoVirtualWorkAll");
}
}

public class Grandson : Son
{
public override void DoVirtualWork()
{
Console.WriteLine("GrandSon.DoVirtualWork");
}

public override void DoVirtualWork2()
{
Console.WriteLine("GrandSon.DoVirtualWork2");
}
public override void DoVirtualWorkAll()
{
base.DoVirtualWorkAll();
Console.WriteLine("GrandSon.DoVirtualWorkAll");
}
}

class Program
{
static void Main(string[] args)
{
Father son = new Son();
Son.DoStaticWork();

son.DoWork();
son.DoVirtualWork();
son.DoVirtualWork2();
son.DoVirtualWorkAll();

Console.WriteLine("--------------------");

Father gson = new Grandson();

gson.DoWork();
gson.DoVirtualWork();
gson.DoVirtualWork2();
gson.DoVirtualWorkAll();

Console.WriteLine("--------------------");

Father father = new Father();

father.DoWork();
father.DoVirtualWork();
father.DoVirtualWork2();
father.DoVirtualWorkAll();

Console.WriteLine("--------------------");

Son realSon = new Son();

realSon.DoWork();
realSon.DoVirtualWork();
realSon.DoVirtualWork2();
realSon.DoVirtualWorkAll();

Console.WriteLine("--------------------");

Son realgSon = new Grandson();

realgSon.DoWork();
realgSon.DoVirtualWork();
realgSon.DoVirtualWork2();
realgSon.DoVirtualWorkAll();
}
}
}

Reference