Export (0) Print
Expand All

Lab 7: Inheritance and Override

Visual Studio .NET 2003
 

Visual Studio Team
Microsoft Corporation

August 2001

Summary: In this hands-on lab, you will build a simple application in Visual Basic .NET that demonstrates the concept of object inheritance. (8 printed pages)

Download the Experience Visual Studio .NET Lab files from the introduction topic.

Contents

Introduction
Inheritance Statements and Modifiers
Inheritance Example 1
Inheritance Example 2
Closing Out of Lab 7
Reset Instructions

Introduction

In this hands-on lab, you will build a simple application that demonstrates the concept of object inheritance.

Microsoft Visual Basic .NET supports inheritance by allowing you to define classes that serve as base classes for other derived classes. The derived classes inherit, and can extend, the properties, methods, events, and data members of the base class. Derived classes can also override inherited methods with new implementations of base methods. All classes are inheritable by default. Forms, which are in fact classes, can also use inheritance to define new forms based on existing forms.

Inheritance Statements and Modifiers

Visual Basic .NET introduces several statements and modifiers to support inheritance. The following table describes them:

Statement/Modifier Description
Inherits Inherits statement—Specifies the class (also known as the base class) from which the current class inherits. The Inherits keyword is only allowed in classes and interfaces.
NotInheritable NotInheritable modifier—Prevents programmers from using the class as a base class.
MustInherit MustInherit modifier—Specifies that instances of the given class cannot be created. The only way to use the class is to inherit from it.
Overridable Overridable modifier—Allows a property or a method to be overridden in an inheriting class. Public methods are NotOverridable by default.
Overrides Overrides modifier—Allows you to override a property or method that is defined in the base class.
NotOverridable NotOverridable modifier (default)—Prevents a property or method from being overridden in an inheriting class.
MustOverride MustOverride modifier—Requires the inheriting class to override the property or method. When the MustOverride keyword is used, the method definition consists of just the Sub, Function, or Property statement. No other statements are allowed and, more specifically, there is no End Sub, End Function, or End Property statement. Classes with MustOverride methods must be declared as MustInherit. Public methods are NotOverridable by default.
Shadows Shadows modifier—Allows reuse of the names of inherited class members. Shadowing a name does not remove the inherited type members by that name; it only makes all of the inherited type members, including overloaded and overridden members, unavailable in the derived class. Shadowing re-declares the class member in the derived class.

Certain combinations of these statements and modifiers cannot be allowed:

  • Overridable and NotOverridable are mutually exclusive and cannot be combined.
  • NotOverridable cannot be combined with Overridable or MustOverride.
  • Overrides implies Overridable and cannot be combined with MustOverride.
  • MustOverride implies Overridable and cannot be combined with NotOverridable.
  • MustOverride methods cannot override other methods, and so cannot be combined with the Overrides modifier.

Inheritance Example 1

You will create a simple executable application using the Microsoft Visual Studio .NET Integrated Development Environment (IDE).

  1. To open the IDE, click Start, click Programs, click Microsoft Visual Studio .NET, click Microsoft Visual Studio .NET. The Start page appears.
  2. To create a new project, click New Project to open the New Project window.
  3. In the Project Types field, click Visual Basic Projects.
  4. In the Templates field, click Empty Project.
  5. Click OK.
  6. To add a class to the project, on the Project menu, click Add Class.
  7. In the Name field, type A.vb.
  8. Click Open. An empty class appears as in Figure 1.

    Figure 1   Empty class

  9. Enter the following code above Public Class A:
    Imports System
    Imports System.Console
    
  10. Add the following code to complete Class A:
    Class A
        Public Sub F()
            Console.WriteLine("A.F")
        End Sub
        
        Public Overridable Sub G()
            Console.WriteLine("A.G")
        End Sub
    End Class
    
  11. Add the following code beneath Class A:
    Class B
        Inherits A
        Public Shadows Sub F()
            Console.WriteLine("B.F")
        End Sub
        
        Public Overrides Sub G()
            Console.WriteLine("B.G")
        End Sub
    End Class
    

    In the following portion of the code, you declare two objects and invoke both of their methods to see the results:

    Class Test
        Shared Sub Main()
            Dim b As New B()
            Dim a As A = b
            a.F()
            b.F()
            a.G()
            b.G()
        End Sub
    End Class
    
  12. Press F5 to run the application. The Output window appears with the results, as in Figure 2.

    Figure 2. Inheritance Example 1 output results

    Note   The results for b.F() and b.G() are to be expected. Notice, however, that the statement a.G() invokes B.G, not A.G. This occurs because we have allowed method G of class A to be overridden. The actual method implementation is determined at run time by method G of class B, which overrides the method inherited from class A. This ability to change the compile time method implementation is what makes inheritance such a powerful feature in object-oriented programming.
  13. Click File, and then click Close Solution.

    Leave the IDE open for the next example.

Inheritance Example 2

This example further demonstrates inheritance.

  1. Click File, click New, and then click Project to open the New Project window.
  2. In the Project Types field, click Visual Basic Projects, and in the Templates field, click Empty Project.
  3. Click OK.
  4. To add a class to the project, on the Project menu, click Add Class.
  5. In the Name field, type A.vb.
  6. Click Open. An empty Class appears.
  7. Enter the following code Above Public Class A:
    Imports System
    Imports System.Console
    
  8. Add the following code to complete class A:
    Class A
        Public Overridable Sub F()
            Console.WriteLine("A.F")
        End Sub 
    End Class 
    
  9. Add the following code beneath Class A:
    Class B
        Inherits A
        Public Overrides Sub F()
            Console.WriteLine("B.F")
        End Sub 
    End Class 
    
    Class C
        Inherits B
        Public Shadows Overridable Sub F()
            Console.WriteLine("C.F")
        End Sub 
    End Class 
    
    Class D
        Inherits C
        Public Overrides Sub F()
            Console.WriteLine("D.F")
        End Sub 
    End Class 
    

    In the following portion of the code, you declare four objects and invoke their methods to see the results:

    Class Test
        Shared Sub Main()
            Dim d As New D()
            Dim a As A = d
            Dim b As B = d
            Dim c As C = d
            a.F()
            b.F()
            c.F()
            d.F()
        End Sub 
    End Class
    
  10. Press F5 to run the application. The Output window appears with the following results as in Figure 3.

    Figure 3. Inheritance Example 2 output results

    Note   In class A there is an overridable method. In class B there is an override method. Class A and class B are quite similar to the previous example. Class B inherits class A and overrides method F, thus changing the run-time implementation of method F. Both a.F() and b.F() invoke B.F, because method F of class A has been overridden by override method F of class B.
    In class C, we have another overridable method, which is also a shadow method. This means that it will shadow the compile-time implementation of the inherited method F, or it can be overridden if another class inherits it. Class D inherits class C. In class D there is an override method that changes the run-time implementation of class C method F. It is because of this that both c.F() and d.F() invoke D.F.
    Because methods are allowed to shadow inherited methods, it is possible for a class to contain several overridable methods with the same signature. However, this does not result in ambiguity, because all but the most derived method is hidden and therefore inaccessible, and there is consequently no naming conflict.
    It is possible to invoke the shadowed overridable method by accessing an instance of D through a less-derived type in which the method is not hidden.

Closing Out of Lab 7

When you have finished viewing the project files, close all the windows.

Other articles and labs in the Experience Visual Studio .NET set include:

Introducing the Visual Studio .NET Lab Series

Lab 1: Hello World

Lab 2: XML Comments

Lab 3: ASP vs. ASP.NET

Lab 4: Server Controls Walkthrough

Lab 5: Using the Visual Basic Upgrade Wizard

Lab 6: Building a Browser-Based Application

Reset Instructions

Your computer can be cleared of all lab-related information by using the HOLT1-01 Experience Visual Studio .NET.msi installer package and the Reset.vbs script file.

Follow the steps below to reset your computer.

  1. Double-click HOLT1-01 Experience Visual Studio .NET.msi. The InstallShield wizard will appear.
  2. Click Next. A page titled Program Maintenance appears.
  3. Select Remove, and then click Next. A window titled Remove the Program appears.
  4. Click Remove. This will remove all lab-related files and folders.
  5. Click Finish.

    You will now remove the shortcuts from the Start menu.

  6. Double-click Reset.vbs. When the Windows Script Host window opens, click OK.

To run through the labs again

  1. Install HOLT1-01 Experience Visual Studio .NET.MSI.
  2. Execute Setup.vbs.
Show:
© 2014 Microsoft