Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / docs / ecma334 / 20.4.3.xml
1 <?xml version="1.0"?>
2 <clause number="20.4.3" title="Interface implementation inheritance">
3   <paragraph>A class inherits all interface implementations provided by its base classes. </paragraph>
4   <paragraph>Without explicitly re-implementing an interface, a derived class cannot in any way alter the interface mappings it inherits from its base classes. <example>[Example: For example, in the declarations <code_example><![CDATA[
5 interface IControl  
6 {  
7    void Paint();  
8 }  
9 class Control: IControl  
10 {  
11    public void Paint() {...}  
12 }  
13 class TextBox: Control  
14 {  
15    new public void Paint() {...}  
16 }  
17 ]]></code_example>the Paint method in TextBox hides the Paint method in Control, but it does not alter the mapping of Control.Paint onto IControl.Paint, and calls to Paint through class instances and interface instances will have the following effects <code_example><![CDATA[
18 Control c = new Control();  
19 TextBox t = new TextBox();  
20 IControl ic = c;  
21 IControl it = t;  
22 c.Paint();    // invokes Control.Paint();  
23 t.Paint();    // invokes TextBox.Paint();  
24 ic.Paint();   // invokes Control.Paint();  
25 it.Paint();   // invokes Control.Paint();  
26 ]]></code_example>end example]</example> </paragraph>
27   <paragraph>However, when an interface method is mapped onto a virtual method in a class, it is possible for derived classes to override the virtual method and alter the implementation of the interface. <example>[Example: For example, rewriting the declarations above to <code_example><![CDATA[
28 interface IControl  
29 {  
30    void Paint();  
31 }  
32 class Control: IControl  
33 {  
34    public virtual void Paint() {...}  
35 }  
36 class TextBox: Control  
37 {  
38    public override void Paint() {...}  
39 }  
40 ]]></code_example>the following effects will now be observed <code_example><![CDATA[
41 Control c = new Control();  
42 TextBox t = new TextBox();  
43 IControl ic = c;  
44 IControl it = t;  
45 c.Paint();    // invokes Control.Paint();  
46 t.Paint();    // invokes TextBox.Paint();  
47 ic.Paint();   // invokes Control.Paint();  
48 it.Paint();   // invokes TextBox.Paint();  
49 ]]></code_example>end example]</example> </paragraph>
50   <paragraph>Since explicit interface member implementations cannot be declared virtual, it is not possible to override an explicit interface member implementation. However, it is perfectly valid for an explicit interface member implementation to call another method, and that other method can be declared virtual to allow derived classes to override it. <example>[Example: For example <code_example><![CDATA[
51 interface IControl  
52 {  
53    void Paint();  
54 }  
55 class Control: IControl  
56 {  
57    void IControl.Paint() { PaintControl(); }  
58    protected virtual void PaintControl() {...}  
59 }  
60 class TextBox: Control  
61 {  
62    protected override void PaintControl() {...}  
63 }  
64 ]]></code_example></example></paragraph>
65   <paragraph>
66     <example>Here, classes derived from Control can specialize the implementation of IControl.Paint by overriding the PaintControl method. end example]</example>
67   </paragraph>
68 </clause>