1 / 14

Visual Basic: An Object Oriented Approach

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

waldo
Download Presentation

Visual Basic: An Object Oriented Approach

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Visual Basic: An Object Oriented Approach 10 - Polymorphism

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. Interfaces • Interface definitions are created to match an application • Classes that implement the interface will ‘fit’ the application Interchangeable Classes Application Interface

  12. 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

  13. 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

  14. CAD Program - result All of these are IShape objects, but are different classes

More Related