class Base { public void F() {} } class Derived: Base { public void F() {} // Warning, hiding an inherited name }the declaration of F in Derived causes a warning to be reported. Hiding an inherited name is specifically not an error, since that would preclude separate evolution of base classes. For example, the above situation might have come about because a later version of Base introduced an F method that wasn't present in an earlier version of the class. Had the above situation been an error, then any change made to a base class in a separately versioned class library could potentially cause derived classes to become invalid. end example] Paragraph 41 The warning caused by hiding an inherited name can be eliminated through use of the new modifier: [Example:
class Base { public void F() {} } class Derived: Base { new public void F() {} }The new modifier indicates that the F in Derived is "new", and that it is indeed intended to hide the inherited member. end example] 2 A declaration of a new member hides an inherited member only within the scope of the new member. [Example:
class Base { public static void F() {} } class Derived: Base { new private static void F() {} // Hides Base.F in Derived only } class MoreDerived: Derived { static void G() { F(); } // Invokes Base.F }In the example above, the declaration of F in Derived hides the F that was inherited from Base, but since the new F in Derived has private access, its scope does not extend to MoreDerived. Thus, the call F() in MoreDerived.G is valid and will invoke Base.F. end example]
| |
Jagger Software Ltd | |
Company # 4070126 | |
VAT # 762 5213 42 |