Member Access and Inheritance


As explained in Chapter 8, members of a class are often declared private to prevent their unauthorized use or tampering. Inheriting a class does not overrule the private access restriction. Thus, even though a derived class includes all of the members of its base class, it cannot access those members of the base class that are private. For example, if, as shown here, width and height are made private in TwoDShape, then Triangle will not be able to access them:

 // Private members are not inherited. // This example will not compile. using System; // A class for two-dimensional objects. class TwoDShape {   double width;  // now private   double height; // now private   public void showDim() {     Console.WriteLine("Width and height are " +                        width + " and " + height);   } } // Triangle is derived from TwoDShape. class Triangle : TwoDShape {   public string style; // style of triangle   // Return area of triangle.   public double area() {     return width * height / 2; // Error, can't access private member   }   // Display a triangle's style.   public void showStyle() {     Console.WriteLine("Triangle is " + style);   } }

The Triangle class will not compile because the reference to width and height inside the area( ) method causes an access violation. Since width and height are now private, they are only accessible by other members of their own class. Derived classes have no access to them.

REMEMBER 

A private class member will remain private to its class. It is not accessible by any code outside its class, including derived classes.

At first, you might think that it is a serious restriction that derived classes do not have access to the private members of base classes, because it would prevent the use of private members in many situations. However, this is not true, because C# provides various solutions. One is to use protected members, which is described in the next section. A second is to use public properties to provide access to private data. As you have seen in the preceding chapters, C# programmers typically grant access to the private members of a class through methods or by making them into properties. Here is a rewrite of the TwoDShape classes that makes width and height into properties:

 // Use properties to set and get private members. using System; // A class for two-dimensional objects. class TwoDShape {   double pri_width;  // now private   double pri_height; // now private   // Properties for width and height.   public double width {      get { return pri_width; }      set { pri_width = value; }   }   public double height {      get { return pri_height; }      set { pri_height = value; }   }   public void showDim() {     Console.WriteLine("Width and height are " +                        width + " and " + height);   } } // A derived class of TwoDShape for triangles. class Triangle : TwoDShape {   public string style; // style of triangle   // Return area of triangle.   public double area() {     return width * height / 2;   }   // Display a triangle's style.   public void showStyle() {     Console.WriteLine("Triangle is " + style);   } } class Shapes2 {   public static void Main() {     Triangle t1 = new Triangle();     Triangle t2 = new Triangle();     t1.width = 4.0;     t1.height = 4.0;     t1.style = "isosceles";     t2.width = 8.0;     t2.height = 12.0;     t2.style = "right";     Console.WriteLine("Info for t1: ");     t1.showStyle();     t1.showDim();     Console.WriteLine("Area is " + t1.area());     Console.WriteLine();     Console.WriteLine("Info for t2: ");     t2.showStyle();     t2.showDim();     Console.WriteLine("Area is " + t2.area());   } }

In this version, the properties width and height provide access to the private members, pri_width and pri_height, which actually store the values. Therefore, even though pri_width and pri_height are private to TwoDShape, their values can still be set and obtained through their corresponding public properties.

When referring to base and derived classes, sometimes the terms superclass and subclass are used. These terms come from Java programming. What Java calls a superclass, C# calls a base class. What Java calls a subclass, C# calls a derived class. You will commonly hear both sets of terms applied to a class of either language, but this book will continue to use the standard C# terms. C++ also uses the base-class/derived-class terminology.

Using Protected Access

As just explained, a private member of a base class is not accessible by a derived class. This would seem to imply that if you wanted a derived class to have access to some member in the base class, it would need to be public. Of course, making the member public also makes it available to all other code, which may not be desirable. Fortunately, this implication is untrue because C# allows you to create a protected member. A protected member is public within a class hierarchy, but private outside that hierarchy.

A protected member is created by using the protected access modifier. When a member of a class is declared as protected, that member is, with one important exception, private. The exception occurs when a protected member is inherited. In this case, a protected member of the base class becomes a protected member of the derived class and is, therefore, accessible by the derived class. Thus, by using protected, you can create class members that are private to their class but that can still be inherited and accessed by a derived class.

Here is a simple example that uses protected:

 // Demonstrate protected. using System; class B {   protected int i, j; // private to B, but accessible by D   public void set(int a, int b) {     i = a;     j = b;   }   public void show() {     Console.WriteLine(i + " " + j);  } } class D : B {   int k; // private   // D can access B's i and j   public void setk() {      k = i * j;   }   public void showk() {     Console.WriteLine(k);   } } class ProtectedDemo {   public static void Main() {     D ob = new D();     ob.set(2, 3); // OK, known to D     ob.show();    // OK, known to D     ob.setk();  // OK, part of D     ob.showk(); // OK, part of D   } }

In this example, because B is inherited by D and because i and j are declared as protected in B, the setk( ) method can access them. If i and j had been declared as private by B, then D would not have access to them, and the program would not compile.

Like public and private, protected status stays with a member no matter how many layers of inheritance are involved. Therefore, when a derived class is used as a base class for another derived class, any protected member of the initial base class that is inherited by the first derived class is also inherited as protected by a second derived class.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

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