Interface inheritance
When you create an abstract class, use the keyword
InterfaceInstead of
Class。 Name the interface, and then define all the properties and methods that require a subclass implementation. This is because there are no properties and methods in the base class that can be implemented, it contains only general data and does not contain methods. What you create is just a contract that stipulates that all subclasses that use this interface must follow certain rules.
1. Now, add a new class to the project that you have created.
2. From the Visual Studio menu, click
Project(project), and then click
ADD Class(Add Class).
3. Add the following code to the class:
4. Interface person
5. Property FirstName () as String
6. Property LastName () as String
7.
8. Sub Print ()
9. Sub Talk ()
End Interface
You will find that you define properties and subroutines in the same way that you typically define these properties and procedures. The only difference is that you don't write any code for them. Now let's see how this interface is used in class definitions.
10. Add the following code to the class file created in the previous step:
One. Public Class Employee
Implements Person
13.
Private Mstrfirstname as String
Private Mstrlastname as String
16.
Property FirstName () as String _
Implements Person.firstname
Get
Return Mstrfirstname
End Get
Set
Mstrfirstname = Value
-End Set
Property
26.
Property LastName () as String _
Implements Person.lastname
Get
Return Mstrlastname
End Get
Set
Mstrlastname = Value
End Set
End Property
36.
Sub Print () Implements Person.print
38. ' Add some code here
A. End Sub
40.
Sub Talk () Implements Person.talk
42. ' Add some code here
End Sub
End Class
The first line after the Employee class definition is Implements person. This keyword indicates that you want to conform to the contract defined in the person interface. You can now define all the properties and methods in the contract. In every
PropertyStatement, you must include the following
Implementskeyword, and you must specify the name of the interface and the name of the method/property that you are using (there is a point between the two names [.] )。 Visual Basic. NET tracks each interface, and you cannot compile the application until all interfaces have been created.
If you want to run the code, you need to create the corresponding child procedure, because in the example above, the child procedures are left blank. After you create all the child procedures, you can declare and use the new Employee object as you would normally create and use any other object.
Select the type of inheritance to use
Sometimes it is very difficult to decide whether to use implementation inheritance or interface inheritance, and in many cases, both types of inheritance may be used, but only a small portion of it is involved. For example, you might want to
LineClass to add a method definition that must be overridden by a quilt class, used in a procedure definition.
MustOverrideKeyword for this operation.
Public MustOverride Sub Init ()
After you add this definition to a class, it acts like an interface. In a subclass, you must define the Init method, and the method must use the
OverridesKey words. Here's how to define
InitExamples of methods:
Public Overrides Sub Init ()
Mstrdelim = ""
Mstrline = "Test Line"
End Sub
Also, keep in mind that you use
OverridesKey words. This keyword is used to notify the compiler that this method overrides the
InitMethod.
Note:The Microsoft. NET Framework Online Help provides a design guide that can help you decide which type of inheritance to use.
Block Inheritance
In some cases, you may not want other classes to inherit your class. If so, you can use the keyword
NotInheritableTo block inheritance of the class.
Public Class NotInheritable Employee
' Class definition
End Class
New features since Visual Basic 6.0
With Visual Basic. NET, you can inherit all the classes contained in the. NET Framework. You can create your own classes that inherit existing classes, and add or remove functionality by making simple changes to your code.
Summarize
This article describes how to inherit a base class, add additional properties to the base class, and use the
OverridesKeyword to replace the functionality defined in the base class. Also describes the use of
MyBaseKeyword to extend the functionality of the base class by calling methods in the base class. Although inheritance does not apply to all applications, inheritance becomes a very powerful tool if used correctly.