JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Using Protected Inheritance

When you declare a member of a base class protected, it's available throughout that class, and in any derived classes, but nowhere else. You can see an example of this in the Inheritance example on the CD-ROM, as discussed in the In Depth section of this chapter. In that example, I pass the main Windows form of the program to the Animal class's constructor (so that class can display text in the main window). That form is stored in the Animal class's MainForm variable. Because derived classes also will need to use MainForm, but no one else will, I made that variable protected (also discussed in the In Depth section of this chapter):

Public Class Form1
    Inherits System.Windows.Forms.Form

    'Windows Form Designer generated code
    Dim spot As Dog

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        spot = New Dog(Me)
        spot.Breathing()
    End Sub
End Class

Public Class Animal
    Protected MainForm As Form1
    Public Sub New(ByVal form1 As Form1)
        MainForm = form1
    End Sub

    Public Sub Breathing()
        MainForm.TextBox1.Text = "Breathing..."
    End Sub
End Class

Public Class Dog
    Inherits Animal

    Public Sub New(ByVal form1 As Form1)
        MyBase.New(form1)
    End Sub

    Public Sub Barking()
        MainForm.TextBox1.Text = "Barking..."
    End Sub
End Class
Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor