Matt Markus...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Matt Markus...

  1. #1
    Joe Schwartz Guest

    Matt Markus...

    Hey thanx...that is the best definition I have heard of yet. I have a couple
    of quick questions though. First, in this example below that you gave me,
    wouldn't the RollOver function actually be written "IDog_RollOver"?
    IDog_Bark, etc??

    Second, what is polymorphism? I thought it was what you described interface
    inheritance as. So what is it, and how does it (if it does at all) relate to
    interface inheritance?

    thanx again

    joe







    "Matt Markus" <matt_markus@hotmail.com> wrote in message
    news:3b1e39df@news.devx.com...
    > Hi Joe --
    >
    > "Joe Schwartz" <gilly@inlink.com> wrote in message
    > news:3b1e1d42@news.devx.com...
    > > Can someone please explain to me what interface inheritance is? Every

    web
    > > page that I go to that defines it, is about 20 pages long. Can someone
    > > please give me as simple as a definition as possible?
    > >
    > > thanx,
    > > joe
    > >
    > >

    >
    > Put simply, Interface Inheritance, in terms of VB, is a class module that
    > inherits or "implements" an interface (another class module) that is
    > composed of only method and property signatures (or events). When I say
    > signatures, I mean function or property procedures with *no*

    implementation
    > code. The class module that inherits this interface agrees that every

    object
    > it serves up will look exactly like the interface that it implements. In
    > other words, this class module cannot have a method or property that the
    > interface it's inheriting doesn't support.
    >
    > Example:
    >
    > IDog interface:
    >
    > Public Sub RollOver(intTimes as Integer)
    > 'no implementation code
    > End sub
    >
    > Public Sub Bark()
    > 'no implementation code
    > End Sub
    >
    > Public Property Get Color() as String
    > 'no implementation code
    > End Property
    >
    > Public Property Let Color(strNewColor As String)
    > 'no implementation code
    > End Property
    >
    >
    > cBeagle class:
    >
    > 'Beagle's are dogs so it will inherit the IDog interface...
    > Implements IDog
    >
    > Private m_str_Color As String
    >
    > Public Sub RollOver(intTimes as Integer)
    > 'implementation code...
    > MsgBox "Dog is rolling over: " & intTimes & "times."
    > End sub
    >
    > Public Sub Bark()
    > 'implementation code...
    > MsgBox "Beagle Bark!"
    > End Sub
    >
    > Public Property Get Color() as String
    > 'implementation code...
    > Color = m_str_Color
    > End Property
    >
    > Public Property Let Color(strNewColor As String)
    > 'implementation code...
    > m_str_Color = strNewColor
    > End Property
    >
    >
    > So, as you can see, any number of dog objects can be written to inherit

    the
    > IDog interface, each with there own unique behavior (i.e., a Beagle will
    > bark differently than a German Shepard, etc).
    >
    >
    > One advantage to Interface Inheritance is that you can upgrade you

    existing
    > objects without breaking compatibility with the clients that use it, while
    > at the same time, introducing new client's to your newer code. For

    example,
    > if you decide you need to change the datatype of your RollOver method of
    > your Beagle object to support rolling over more than 32,767 times, you

    must
    > make that function accept a Long instead of an Integer. In a world without
    > interfaces, this, as you know, will break compatibility with clients that
    > have been compiled against your beagle object. So what do you do in
    > Interface Programming? Well, you add another interface, say, called IDog2,
    > whose RollOver method signature accepts a long as a parameter. Then, you

    add
    > "Implements IDog2" to your Beagle class, and that object will support both
    > the IDog and IDog2 interfaces, and you won't have to recompile your client
    > against a new object. Now you can write new client code that queries an
    > object to see if it supports a certain interface, and if it doesn't, can
    > just use the old version. Example:
    >
    > Sub MakeDogRollOver(ByVal objDog as IDog)
    >
    > If TypeOf objDog Is IDog2 Then
    > 'use the new version
    > Dim objDog2 as IDog2
    > Set objDog2 = objDog
    > objDog2.RollOver(50000)
    > Else
    > 'use older version
    > objDog.RollOver(32000)
    > End If
    > End Sub
    >
    >
    > The big thing about this versioning scheme is that it allows you to make
    > changes to your objects without affecting the code that's already in
    > production.
    >
    >
    > Note: this type of programming isn't used that much in VB, based on my
    > experience.
    >
    >
    > Hope this makes a little sense, being that I don't fully understand it
    > either.
    >
    >






  2. #2
    Matt Markus Guest

    Re: Matt Markus...

    Hi Joe --

    > Hey thanx...that is the best definition I have heard of yet. I have a

    couple
    > of quick questions though. First, in this example below that you gave me,
    > wouldn't the RollOver function actually be written "IDog_RollOver"?
    > IDog_Bark, etc??


    Yes, the functions in the concrete class that is implementing the IDog
    interface must be prefixed with the name of the interface (i.e.,
    IDog_RollOver). After you type 'Implements IDog', you'll see the name of the
    interface in the upper left-hand combo-box of the code window. If you choose
    that, you'll see, in the right-hand combo-box, all of the method and
    property definitions of the interface. If you choose one, VB will
    automatically create the definition for you, including the prefix.

    > Second, what is polymorphism? I thought it was what you described

    interface
    > inheritance as. So what is it, and how does it (if it does at all) relate

    to
    > interface inheritance?
    >
    > thanx again


    A simple defintion for polymorphism would be: the ability for a client to
    use different object's in the same way, even if they offer different
    behavior, or we're created from different classes.

    For example, in my ealier post, when I said: "So, as you can see, any number
    of dog objects can be written to inherit the
    IDog interface, each with there own unique behavior (i.e., a Beagle will
    bark differently than a German Shepard, etc)", I was describing the ability
    for polymorhpic behavior:

    Dim objGermanShepard As IDog
    Dim objBeagle As IDog
    Dim objCollie As IDog
    Dim colDogs As Collection
    Dim oDog as IDog

    Set objGermanShepard = New cShepard
    Set objBeagle = New cBeagle
    Set objCollie = New cCollie

    Set colDogs = New Collection
    ColDogs.Add objGermanShepard
    ColDogs.Add objBeagle
    ColDogs.Add objCollie

    For each oDog in colDogs
    oDog.Bark() 'get a different response based on the current object's
    implementation of Bark()
    Next

    Here I created a collection of dog objects, all of which are accessed
    through the IDog interface, each with their own implementation of the Bark()
    method. And I know that any IDog-Compatible object will have a Bark() method
    with the same calling syntax so I can treat them in the same manner.

    So, to put this in perspective, say you wrote an application that maintains
    a collection of IDog-compatible objects; since the 'for each loop' above is
    written in terms of the IDog interface, it has no dependencies on any
    concrete class, and therefore will not need to be altered when new
    IDog-compatible objects are introduced.

    I hope that illustrated polymorphism and why it's considered the most
    important concept in OOP.

    Matt




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center