A lot of keywords to define polymorphic behavior, classes, and methods that can be overridden; members that may or may not be accessed are discussed in Chapter 4, “Inheritance.” The functionality of C#, C++/CLI and Visual Basic is very similar, but the keywords are different.
The access modifiers of C++/CLI and Visual Basic are very similar to C#, with some notable differences. Visual Basic uses the Friend access modifier instead of internal for accessing the types in the same assembly. C++/CLI has one more access modifier: protected private. internal protected allows accessing the members from within the same assembly, and also from other assemblies if the type is derived from the base type. C# and Visual Basic don’t have a way to allow only derived types within the same assembly. This is possible with protected private from C++/CLI. Here private means that outside the assembly there’s no access, but from inside the assembly protected access is possible. The order - whether you write protected private or private protected - does not matter. The access modifier allowing more is always located within the assembly, and the access modifier allowing less is always outside of the assembly.
C# | C++/CLI | Visual Basic |
---|---|---|
public | Public | Public |
protected | Protected | Protected |
private | Private | Private |
internal | Internal | Friend |
internal protected | internal protected | Protected Friend |
not possible | protected private | not possible |
Keywords important for inheritance are mapped in the following table.
C# | C++/CLI | Visual Basic | Functionality |
---|---|---|---|
: | : | Implements | Implement an interface |
: | : | Inherits | Inherits from a base class |
virtual | virtual | Overridable | Declare a method to support polymorphism |
overrides | override | Overrides | Override a virtual method |
new | new | Shadows | Hide a method from a base class |
abstract | abstract | MustInherit | Abstract class |
sealed | sealed | NotInheritable | Sealed class |
abstract | abstract | MustOverride | Abstract method |
sealed | sealed | NotOverridable | Sealed method |
this | this | Me | Reference the current object |
base | Classname:: | MyBase | Reference the base class |
The order in which you place the keywords is important in the languages. In the code sample, an abstract base class Base with one abstract method and one implemented method that is virtual are defined. The class Base is derived from the class Derived, where the abstract method is implemented, and the virtual method is overridden:
// C# public abstract class Base { public virtual void Foo() { } public abstract void Bar(); } public class Derived : Base { public override void Foo() { base.Foo(); } public override void Bar() { } } // C++/CLI public ref class Base abstract { public: virtual void Foo() { } virtual void Bar() abstract; }; public ref class Derived : public Base { public: virtual void Foo() override { Base::Foo(); } virtual void Bar() override { } }; ' Visual Basic Public MustInherit Class Base Public Overridable Sub Foo() End Sub Public MustOverride Sub Bar() End Class Public class Derived Inherits Base Public Overrides Sub Foo() MyBase.Foo() End Sub Public Overrides Sub Bar() End Sub End Class