VB supports full inheritance as per the OOP specification. It does not support multiple inheritance. Multiple inheritance is where a class can inherit from more than one base class, effectively merging the functionality of the two. There has been much discussion in academic circles as to whether this should even be included in the OOP specification. It can be confusing and even dangerous so Microsoft left it out of VB. It is available in C++ if you really must use it. To create a class that inherits from a base class use the Inherits keyword in the declaration of a class.
Public Class MyClass Inherits System.Windows.Forms.Form End Class
This class declaration creates a class called MyClass, which inherits the Windows.Forms.Form class. In other words, this creates a new form class. The inherits clause must be on the next line of text after the class declaration or you will generate a syntax error. Classes do not have to inherit from any other class.
Public Class MyClass End Class
This declaration is perfectly valid. If inherited from it will become the root class of a new inheritance tree.
Classes can be defined that cannot be inherited from:
Public NotInheritable Class MyClass Inherits System.Windows.Forms.Form End Class
This is useful if you are writing some classes you wish to sell commercially and you don't want your customers pirating your code by inheriting from it and then selling a new product. It is also useful in a secure environment where you don't want the users of your class to override functionality.
Classes can also be defined that must be inherited from:
Public MustInherit Class MyClass Inherits System.Object End Class
This creates an abstract class. These are classes you want to use as a template for defining other classes. Abstract classes should only inherit from other abstract classes. If you try to create an abstract class by inheriting a nonabstract class, it will allow it, but you will not be able to use it in the IDE designer. You will get an error saying that it cannot instantiate the abstract class.
Constructors and Finalizers
Constructors and finalizers are special procedures that a class uses to create and destroy itself. Every class must have at least one constructor, called a default constructor. If you do not provide one, VB assumes there is a blank constructor. Without at least one public constructor, the class could never be instantiated. Sometimes this behavior is desirable if you don't want the class to have the ability to be instantiated directly. In VB, the constructor must be a subprocedure named New. In other languages, the constructor(s) must have the same name as the class. I do not know why Microsoft did not continue this convention. There is one advantage to it; when you change the name of a class you do not have to remember to rename every constructor as well. For that reason, perhaps it is a better way of naming constructors. Constructors may be overloaded. As a matter of convention, most OOP programmers create at least one constructor with no parameters, but this is not required. Inherited classes must declare their own constructor, even if one is declared in its base class. To use the base class's constructor, you must explicitly call it like this:
If used, it must be the first line of code in the constructor. If you look at the way constructors are implemented, you are really calling the class's constructor when you use the New keyword. Suppose we have a class:
Public Class ClassA Private intA as Integer Public Sub New() IntA = 1 End Sub End Class
When we create an instance of the class using the New keyword, we are really calling the new procedure:
Private mA as New ClassA() Or Private mA as ClassA mA = New ClassA()
We could use the constructor to pass in initialization data. This is used very often:
Public Class ClassA Private intA as Integer Public Sub New() IntA = 1 End Sub Public Sub New(ByVal IntIN as Integer) IntA = IntIn End Sub End Class Private mA as New ClassA(12)
In this example, we have overloaded the constructor. If the programmer uses the default constructor, intA gets assigned a default value of one. If the programmer uses the second constructor, he can pass in any value he wants to initialize intA.
Finalizers are typically used to clean up before a class is destroyed. In other OOP languages, these are used to deallocate memory, close files, and so forth. In C++ they are called destructors and are named by using a tilde (~) followed by the class name. In VB .NET they are named Finalize and cannot have any parameters. Finalizers cannot be overloaded because they are called implicitly and not under programmer control. In most cases finalizers are not needed in VB .NET because the CLR uses a garbage collection mechanism to automatically deallocate memory and generally clean up after a class is destroyed. Even if you use a finalizer, you still cannot be sure an object has been fully destroyed by the garbage collector.
Public Sub Finalize() ' Destroy something End Sub
The finalizer will be called automatically when the use count for the object drops to zero. Finalizers cannot be called under programmer control.