dlr bug
[mcs.git] / docs / ecma334 / 8.7.11.xml
blob2bae35bf62e65156aafc94dcdd2bf6b388762e2a
1 <?xml version="1.0"?>
2 <clause number="8.7.11" title="Inheritance" informative="true">
3   <paragraph>Classes support single inheritance, and the type object is the ultimate base class for all classes. </paragraph>
4   <paragraph>The classes shown in earlier examples all implicitly derive from object. The example <code_example><![CDATA[
5 using System;  
6 class A  
7 {  
8    public void F() { Console.WriteLine("A.F"); }  
9 }  
10 ]]></code_example>shows a class A that implicitly derives from object. The example <code_example><![CDATA[
11 class B: A  
12 {  
13    public void G() { Console.WriteLine("B.G"); }  
14 }  
15 class Test  
16 {  
17    static void Main() {  
18       B b = new B();  
19       b.F();    // Inherited from A  
20       b.G();      // Introduced in B  
21       
22       A a = b;     // Treat a B as an A  
23       a.F();  
24    }  
25 }  
26 ]]></code_example>shows a class B that derives from A. The class B inherits A's F method, and introduces a G method of its own. </paragraph>
27   <paragraph>Methods, properties, and indexers can be virtual, which means that their implementation can be overridden in derived classes. The example <code_example><![CDATA[
28 using System;  
29 class A  
30 {  
31    public virtual void F() { Console.WriteLine("A.F"); }  
32 }  
33 class B: A  
34 {  
35    public override void F() {   
36       base.F();  
37       Console.WriteLine("B.F");   
38    }  
39 }  
40 class Test  
41 {  
42    static void Main() {  
43       B b = new B();  
44       b.F();  
45       A a = b;   
46       a.F();  
47    }  
48 }   
49 ]]></code_example>shows a class A with a virtual method F, and a class B that overrides F. The overriding method in B contains a call, base.F(), which calls the overridden method in A. </paragraph>
50   <paragraph>A class can indicate that it is incomplete, and is intended only as a base class for other classes, by including the modifier abstract. Such a class is called an abstract class. An abstract class can specify abstract members-members that a non-abstract derived class must implement. The example <code_example><![CDATA[
51 using System;  
52 abstract class A  
53 {  
54    public abstract void F();  
55 }  
56 class B: A  
57 {  
58    public override void F() { Console.WriteLine("B.F"); }  
59 }  
60 class Test  
61 {  
62    static void Main() {  
63       B b = new B();  
64       b.F();  
65       A a = b;  
66       a.F();  
67    }  
68 }  
69 ]]></code_example>introduces an abstract method F in the abstract class A. The non-abstract class B provides an implementation for this method. </paragraph>
70 </clause>