C# Abstract
C# abstract keyword indicates something is incomplete and must be completed in the non-abstract derived class.
- An abstract class is always used as a base class and cannot be instantiated directly.
- An abstract class may contain abstract methods and properties and of course it can contain non-abstract members as usual.
- An abstract class cannot be sealed or static.
- If a non-abstract class inherits from an abstract class, all the inherited methods and properties must be implemented.
- The abstract keyword must be placed in front of the keyword "class".
The syntax of an abstract class can be shown below.
[<access modifier>] abstract class <Class Name> { ... } or abstract [<access modifier>] class <Class Name> { ... }
- Abstract Methods can only be declared in abstract classes.
- An abstract method is implicitly a virtual method which can be overridden in it derived class with the keyword override.
- The static or virtual keyword cannot be used in an abstract method otherwise an error will be issued.
- The abstract keyword must be placed in front of the returned type of the abstract method.
The syntax of an abstract method is like below.
<access modifier> abstract <return type> <Method Name>(); or abstract <access modifier> <return type> <Method Name>();
There is no curly brackets above to define the implementation of an abstract method.
Example 01-66-01
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
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
using System; public abstract class A { } public abstract class B { public int i; public void method1() { Console.WriteLine("method1"); } } public abstract class C { public int i; public void method1() { Console.WriteLine("method1"); } public abstract void method2(); } public abstract class D : C { public abstract void method3(); } public class E : D { public override void method2() { Console.WriteLine("method2"); } public override void method3() { Console.WriteLine("method3"); } } public class TestAbstract { static void Main() { E e = new E(); e.method1(); e.method2(); e.method3(); //B b = new B(); Console.Read(); } }
Output
method1 method2 method3
Explanation
- Line 3-5: It is legal to define an empty abstract class A.
- Line 7-15: It is also legal but not recommended to define an abstract class B without any abstract members.
- Line 17-27: The abstract class C is defined the same as class B except adding an abstract method2().
- Line 29-32: Class D is derived from class C plus adding a new abstract method method3(). There are no implementations in both method2 and method3 because they are all abstract.
- Line 34-45: Class E is inherited from class D and implements all the abstract methods. The override keyword is needed here.
- Line 47-59: Instantiate class E and call the 3 methods.
- Line 56: Commented out because the abstract class B cannot be instantiated.