|
|
Module 10: Inheritance
in C#
This course is based on the prerelease Beta 1 version of Microsoft? Visual Studio .NET.
Content in the final release of the course may be different from the content included in this
prerelease version. All labs in the course are to be completed with the Beta 1 version of
Visual Studio .NET.
Information in this document is subject to change without notice. The names of companies,
products, people, characters, and/or data mentioned herein are fictitious and are in no way intended
to represent any real individual, company, product, or event, unless otherwise noted. Complying
with all applicable copyright laws is the responsibility of the user. No part of this document may
be reproduced or transmitted in any form or by any means, electronic or mechanical, for any
purpose, without the express written permission of Microsoft Corporation. If, however, your only
means of access is electronic, permission to print one copy is hereby granted.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.
ó 2001 Microsoft Corporation. All rights reserved.
Microsoft, ActiveX, BizTalk, IntelliSense, JScript, Microsoft Press, MSDN, PowerPoint, Visual
Basic, Visual C++, Visual #, Visual Studio, Windows, and Windows Media are either registered
trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries.
Other product and company names mentioned herein may be the trademarks of their respective
owners.
4 Module 10: Inheritance in C#
Accessibility of a Derived Class
A derived class cannot be more accessible than its base class. For example, it is
not possible to derive a public class from a private class, as is shown in the
following code:
class Example
{
private class NestedBase { }
public class NestedDerived: NestedBase { } // Error
}
The C# syntax for deriving one class from another is also allowed in C++,
where it implicitly specifies a private inheritance relationship between the
derived and base classes. C# has no private inheritance; all inheritance is public.
Module 10: Inheritance in C# 5
Accessing Base Class Members
n Inherited Protected Members Are Implicitly Protected in the Derived
Class
n Methods of a Derived Class Can Access Only Their Inherited Protected
Members
n Protected Access Modifiers Cannot Be Used in a Struct
class Token
{ ... class Outside
protected string name; {
} void Fails(Token t)
class CommentToken: Token {
{ ... ...
public string Name( ) t.name
{ ...
return name; }
} }
}
class Token
{ ... class Outside
protected string name; {
} void Fails(Token t)
class CommentToken: Token {
{ ... ...
public string Name( ) t.name
{ ...
return name; }
} }
}
?
ü
The meaning of the protected access modifier depends on the relationship
between the class that has the modifier and the class that seeks to access the
members that use the modifier.
Members of a derived class can access all of the protected members of their
base class. To a derived class, the protected keyword behaves like the public
keyword. Hence, in the code fragment shown on the slide, the Name method of
CommentToken can access the string name, w hich is protected inside Token.
It is protected inside Token because CommentToken has specified Token as
its base class.
However, between two classes that are not related by a derived-class and baseclass
relationship, protected members of one class act like private members for
the other class. Hence, in the other code fragment shown on the slide, the Fails
method of Outside cannot access the string name, which is protected inside
Token because Outside does not specify Token as its base class.
6 Module 10: Inheritance in C#
Inherited Protected Members
When a derived class inherits a protected member, that member is also
implicitly a protected member of the derived class. This means that protected
members are accessible to all directly and indirectly derived classes of the base
class. This is shown in the following example:
class Base
{
protected string name;
}
class Derived: Base
{
}
class FurtherDerived: Derived
{
void Compiles( )
{
Console.WriteLine(name); // Okay
}
}
Protected Members and Methods
Methods of a derived class can only access their own inherited protected
members. They cannot access the protected members of the base class through
references to the base class. For example, the following code will generate an
error:
class CommentToken: Token
{
void AlsoFails(Token t)
{
Console.WriteLine(t.name); // Compile-time error
}
}
Many coding guidelines recommend keeping all data private and using
protected access only for methods.
Protected Members and structs
A struct does not support inheritance. Consequently, you cannot derive from a
struct, and, therefore, the protected access modifier cannot be used in a struct.
For example, the following code will generate an error:
struct Base
{
protected string name; // Compile-time error
}
Tip |
|