Results 1 to 4 of 4

Thread: Polymorphism - explanation?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    Polymorphism - explanation?

    Hi,

    Can someone point me in the direction of an article that explains what polymorphism is with regards to its use in VB.Net?

    I don't need to know how to use it.......yet......I just need a clear explanation as to what it is. Please bear in mind that I've only been learning VB for a few weeks and so don't want to be baffled by a load of technical talk!

    Thanks
    -Rob

  2. #2

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    One of the notions of Polymorphism (Which literally means many shapes) if this.

    VB Code:
    1. Class Mouth
    2.     Sub Speak()
    3.         Console.WriteLine("Hello!")
    4.     End Sub
    5.  
    6.     Sub Speak(ByVal Words As String)
    7.         Console.WriteLine(Words)
    8.     End Sub
    9.  
    10.     Sub Speak(ByVal Number As Integer)
    11.         Console.WriteLine(Number)
    12.     End Sub
    13. End Class
    14.  
    15. ...
    16.  
    17. Sub Main()
    18.     Dim x as Mouth = new Mouth
    19.     x.Speak("My Name is X") 'will automatically call the second version of Sub Speak()
    20. End Sub

    The framework knows what kind of data is being passed into the Speak sub and chooses the right variant to do the job.

    But that is only a simple example. Inheritance can expand on this as can Encapsulation.

    NOTE: You might also notice that Console.Writeline() is called using the same technique, the framework knows what data is incoming and acts accordingly.
    I don't live here any more.

  4. #4
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    If you want a more hands-on usage of polymorphism, the GOF pattern "state" uses it... Check out the definition of that pattern... It shows quite well how polymorphish works in practice.

    kind regards
    henrik

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width