140 likes | 234 Views
Visual Basic: An Object Oriented Approach. 10 - Polymorphism. Polymorphism. The facility that permits objects of different classes to be interchangeable Different classes of object can have properties and methods that are compatible Same names Same parameters
E N D
Visual Basic: An Object Oriented Approach 10 - Polymorphism
Polymorphism • The facility that permits objects of different classes to be interchangeable • Different classes of object can have properties and methods that are compatible • Same names • Same parameters • Same return types (for Functions or Property Gets) • Considering only these methods, it is possible to send the same sequence of messages to objects of either class • These classes are polymorphs
Polymorphism Example • This code can be applied to several different types of VB Controls • CommandButtons • Labels • CheckBoxes • Note parameter type – Object • A generalism • All types of VB object can be referred to by this type of object variable • Controls • User defined classes Sub ChangeObject(O As Object) O.Caption = “New Caption” O.BackColor = vbRed O.Width = 5000 O.Move 1000, 1000 End Sub
More general – less secure • We could also have sent a TextBox control to the example code shown • This has no Caption property • The code would cause an error that could only be detected at run-time • Using Object as a general type should be considered bad practice • Ideally, we should restrict the type to one that has all of the properties and methods used in the code • Alternatively, we can check the object type before we access a property or method
Preventing Wrong-Type errors • This could get very tedious, and is prone to errors Sub ChangeObject(O As Object) If TypeOf(O) Is Label Or TypeOf (O) Is CommandButton Then O.Caption = “New Caption” ElseIf TypeOf(O) Is TextBox Then O.Text = “New Text” Else ‘ Does not have a caption or text property, best leave it alone End If ‘ ‘ ‘ ‘ End Sub
Ideally we need Type-Safety • Some way of stating that a class of objects is compatible with some general class • Interfaces provide this security • An interface is nothing more than a definition of Properties and Methods with no executable code involved • Name of Property/Method • Parameters (arguments) passed to them • Values returned from them
Example of a Type Definition • Obvious feature – all procedures are empty • Purely a statement of how a Property, Sub or Function would be called by other code • A Message vocabulary • This is placed in a class, but called an Interface ‘ IData Interface class Public Sub StoreData(DataFile As Integer) End Sub Public Function LoadData(DataFile As _ Integer) End Function Public Property Get Data() As Variant End Property Public Property Set Data(NewValue As _ Variant) End Property
Type Declaration • A class can declared so it Implements an Interface • It can then be guaranteed to provide a set of Procedures/Methods • There is no guarantee that these do anything sensible – just that they are compatible • This is Interface Inheritance
e.g. An implementing class Implements IData ‘ This class must conform to IData Private … ‘ Declare member variables for class Public Sub IData_StoreData(DataFile As Integer) ‘ Does something to store information End Sub Public Function IData_ LoadData(DataFile As Integer) ‘ Does something to load information End Function Public Property Get IData_ Data() As Variant ‘ Returns an internal value End Property Public Property Set IData_ Data(NewValue As Variant) ‘ Updates an internal value End Property
Implementing class – key features • VB Automatically provides templates of Properties and Methods to match interface • No need to type (or mistype) • Programmer is responsible for coding how a given class will implement a specific interface function • Client program can use any class that implements the interface interchangeably
Interfaces • Interface definitions are created to match an application • Classes that implement the interface will ‘fit’ the application Interchangeable Classes Application Interface
Typical example of polymorphism • Computer Aided Drawing (see chapter 10 Exercise) • The program knows how to draw, move and edit an IShape object • There are no IShape objects • Several ‘concrete’ shape classes implement the IShape interface the drawing program uses • The program can therefore draw, move etc. instances of these classes, even though there is no code in it that refers to them
CAD Example This is the abstract class, or superclass Private Sub DrawShapes() Dim I As IShape P.Cls For Each I In DrawList I.Draw P Next End Sub This is a collection of shape objects of various classes This statement draws a shape on PictureBox P, no matter what class it belongs to
CAD Program - result All of these are IShape objects, but are different classes