Share via


How to: Create Derived Classes 

The Inherits statement causes a class to inherit all the non-private members of the specified class.

To inherit from another class

  • Add an Inherits statement with the name of a class you want to use as a base class as the first statement in your derived class. The Inherits statement must be the first non-comment statement after the class statement.

Example

The following example defines two classes. The first class is a base class that has two methods. The second class inherits both methods from the base class, overrides the second method, and defines a field named Field.

Class Class1
    Sub Method1()
        MsgBox("This is a method in the base class.")
    End Sub
    Overridable Sub Method2()
        MsgBox("This is another method in the base class.")
    End Sub
End Class

Class Class2
    Inherits Class1
    Public Field2 As Integer
    Overrides Sub Method2()
        MsgBox("This is a method in a derived class.")
    End Sub
End Class

Protected Sub TestInheritance()
    Dim C1 As New Class1
    Dim C2 As New Class2
    C1.Method1() ' Calls a method in the base class.
    C1.Method2() ' Calls another method from the base class.
    C2.Method1() ' Calls an inherited method from the base class.
    C2.Method2() ' Calls a method from the derived class.
End Sub

When you run the procedure TestInheritance, you see the following messages:

This is a method in the base class.

This is another method in the base class.

This is a method in the base class.

This is a method in a derived class.

See Also

Concepts

Overriding Properties and Methods
Override Modifiers

Other Resources

Inheritance in Visual Basic
Class Properties, Fields, and Methods