Saturday, May 17, 2008

Inheritance in Visual Basic

Inheritance

In the pure world of OOP, objects can be created from other objects. Any properties and methods from one class are then automatically available as properties and methods of the new class. This is called Inheritance.

Visual Basic provides a modified version of Inheritance commonly termed Containment. A class can still expose its childrens' properties and methods but the developer of the class must explicitly reference the child in any calls made to those properties or methods. A comparison is the best way of explaining this concept.

Figure 1. C++ directly uses Person.Name when Student.Name is called

Figure 2. VB must explicitly call Person.Name when Student.Name is called

Let's look at how you would code this :
'Class : clsStudent
'Contains a reference to a Person Object
'private member variables


Private oPerson as clsPerson
Private iGrade as Integer

Public Sub Create ( )
'load this object and create the person object

Set oPerson = New clsPerson
End Sub
Public Property Let Name ( sNewName as String)
'set the person objects Name Property

oPerson.Name = sNewName
End Property

Public Property Get Name ( ) as String
'retreive the person objects Name Property

Name = oPerson.Name
End Property


No comments:

Post a Comment