20.3 Inheritance of properties


Like other class members , properties can be inherited to subclasses. You can override properties just as you would override other methods . It is possible to just override the get section of a property and inherit the set section, and vice versa.

The example below shows two classes, Parent and Child . Child is the subclass of Parent and inherits both the protected field, MyColor , and the public property, Color .

 1: using System;  2:  3: public class Parent{  4:   protected string MyColor = "yellow";  5:  6:   // property Color  7:   public  virtual  string Color{  8:     get{  9:       Console.WriteLine("running GET of Parent"); 10:       return MyColor; 11:     } 12:     set{ 13:       Console.WriteLine("running SET of Parent"); 14:       MyColor = value; 15:     } 16:   } 17: } 18: 19: public class Child:Parent{ 20:   public  override  string Color{ 21:     get{ 22:       Console.WriteLine("running GET of Child"); 23:       return "dark " + MyColor; 24:     } 25:     // the set section will be inherited 26:   } 27: } 28: 29: public class TestMain{ 30:   public static void Main(){ 31:     Child c = new Child(); 32:     Console.WriteLine(c.Color); // get 33:     Console.WriteLine(" -------------- "); 34:     c.Color = "blue";           // set 35:     Console.WriteLine(" -------------- "); 36:     Console.WriteLine(c.Color); // get 37:   } 38: } 

Output:

 C:\expt>test running GET of Child dark yellow -------------- running SET of Parent -------------- running GET of Child dark blue 

Lines 3 “ 17 codes a Parent class that contains a public Color property, which represents the protected field MyColor . This property member contains both the get and set sections, to allow external parties to retrieve and alter the value stored in the private field.

Note that the virtual keyword is used to declare this property (line 7) in Parent . In C#, you must declare a property (and any member of a class) as virtual if you intend to override this property in a subclass. On line 20 in the Child class, the override keyword tells the compiler that this new property (declared on lines 21 “ 26) is overriding the one in the Parent class.

In this example, only the get section of Color has been overridden (lines 21 “ 24) in the Child class. The set section will be inherited automatically. Conversely, if you implement only the set section of a property in the Child class, the get section will be inherited automatically.

Let's examine the output. I have purposely inserted lines 33 and 35 so that we can see which printout is the result of which statement. When statement 32 executes, the get section of the Color property written in the Child class runs. When statement 34 executes, the set section of the Color property inherited by the Child class runs, and alters the value of protected field Color , which has been inherited to Child too.



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net