Results 1 to 7 of 7

Thread: Polymorphism

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    167

    Question Polymorphism

    I have read the book about OOP. It is said that Polymorphism is the ability to have the same method(the same name) in many classes.

    I want to know that if I create two classes and each class has the same print method without using Inheritance or Interface. Is it called Polymorphism or not?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Polymorphism

    A polymorphic method is one that is declared in one class and overridden in another. If two methods in different classes have the same name but no other relationship then that is not polymorphism.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Polymorphism

    Either method overriding, or the ability to implement the same interface method differently, are considered polymorphism.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    167

    Re: Polymorphism

    Thank jmcilhinney and penagate.

    Sometimes my problem has been solved, but I want to get more ideas about this. And I also don't know how to mark it as resolved.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Polymorphism

    Maybe the FAQ can help you

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Polymorphism

    VB Code:
    1. class Parrot
    2. public sub Speak()
    3. ...
    4. end class
    5.  
    6. class Human
    7. public sub Speak()
    8. ...
    9. end class

    This is NOT polymorphism because the two classes are totally unrelated.

    However...

    VB Code:
    1. Class Mammal
    2.         Public Overridable Sub Speak()
    3.         End Sub
    4.     End Class
    5.  
    6.     Class Human
    7.         Inherits Mammal
    8.         Public Overrides Sub Speak()
    9.         End Sub
    10.     End Class

    IS polymorphism because Speak can be called on either a mammal (monkey say "ook") or a Human ("Hi, I'm a car salesman") without the compiler necessarily knowing at runtime which class type an object will be.

    This also shows inheritance.
    I don't live here any more.

  7. #7
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770

    Re: Polymorphism

    Exactly! My professor keeps say that "Polymorphism is the ability to have the same method(the same name) in many classes."

    This could only be the case when inheritance is used, like the example wossname has shown.

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