Common Type System—Implementation Inheritance


Jump to: navigation, search
Visual C# Tutorials
.NET Framework Tutorials

Common Type System

© 2006 Wiley Publishing Inc.

Implementation Inheritance

This is the default inheritance scheme in the CTS whenever you create a subclass of another type. Implementation inheritance means that a subclass gets a copy of all of its base type’s nonprivate members, including method implementations. So the methods defined on the base type, for example, are immediately callable on an instance of the subclass as you would expect. Depending on whether methods are virtual, a subclass might choose to override these methods to supply their own implementation instead of keeping the base type’s existing version. Virtual methods and overriding are topics we discuss in more detail shortly.

As an example of subclassing, consider two types A and B:

class A
{
   public void Foo()
   {
      Console.WriteLine(“A::Foo);
   }
}
 
class B : A
{
   public void Bar()
   {
      Console.WriteLine(“B::Bar);
   }
}

Here, B derives from A—using the <Subclass> : <BaseType> syntax—inheriting the public method Foo and extending A by adding a new method Bar. The result is that the type A has a single method Foo, and the type B has two methods, Foo and Bar. Calling Foo on an instance of B just works, and does the same exact thing as A’s version of Foo (prints out "A::Foo")—the creator of B didn’t have to do anything special to enable this. This is a result of the subclass inheriting both the interface and implementation of the base type’s definition of Foo.


Previous_Page_.gif Next_Page_.gif





Personal tools