Inheritance

In a nutshell, inheritance is the ability to create new classes from existing ones. A derived class, or subclass, inherits the properties and methods of the class that instantiated it (called the base class, or superclass), and may add new properties and methods. New methods can be defined with the same name as those in the superclass, in which case they override the original one.

There are two types of inheritance: interface and implementation inheritance. Interface inheritance has been available to Access since VBA 6.0 introduced the Implements keyword. Implementation inheritance is now available in Visual Basic .NET through the Inherits keyword, but unfortunately not in Access 2003.

The essential difference between the two forms of inheritance is that interface inheritance specifies only the interface. It doesn't actually provide any corresponding implementation code. For example, suppose we have a Bike object that wants to ask the Wheel object for its part number. The Wheel object wants to borrow the functionality from its superclass, Parts. The Bike object might implement the following functionality:

Private myWheel As Wheel

Private Function GetPartNo() As String GetPartNo = myWheel.PartNo() End Function

The implementation of this behavior is in Parts' PartNo() method. Because VBA 6.0 doesn't support implementation inheritance, you would need to put some code into the Wheel class.

Implements Part

Private MyPart As New Part

Private Function Part_PartNo() As String Part_PartNo = myPart.PartNo() End Function

VBA 6.0 allows the interface, in this case Part, to implement the actual behavior. Wheel retains an instance of Part (a behavior called containment), and then asks that reference to carry out some action for it (called delegation). This isn't true interface inheritance because it allows you to add code to Wheel to provide the actual behavior, but it's close enough.

0 0

Post a comment