What is polymorphism?

Results 1 to 9 of 9

Thread: What is polymorphism?

  1. #1
    RBliss Guest

    Default What is polymorphism?

    Hello, let me first start saying I am new to object oriented programming. I finished my book, and I think I have a handle on all topics, except for polymorphism. I am trying to make a test project using all of these conepts, but can not get that one down. Can anyone show me how to use it with an easy example?

  2. #2
    From MSDN Guest

    Default RE: What is polymorphism?

    Polymorphism means that many classes can provide the same property or method, and a caller doesn&#039;t have to know what class an object belongs to before calling the property or method.<BR><BR>For example, a Flea class and a Tyrannosaur class might each have a Bite method. Polymorphism means that you can invoke Bite without knowing whether an object is a Flea or a Tyrannosaur — although you&#039;ll certainly know afterward.<BR><BR>The following topics describe Visual Basic&#039;s approach to polymorphism and how you can use it in your programs. <BR><BR>

  3. #3
    Join Date
    Dec 1969

    Default Poly who ism?

    Well, don&#039;t feel bad, because I found that to be one of the toughest topics also, coming from a VB background. I&#039;m assuming you&#039;re not using VB, since it doesn&#039;t support it. Since you&#039;ve read some things, you should be familar with some of what I&#039;m talking about.<BR><BR>You know about inheritance, right? Well, let&#039;s say in the super class (the "daddy" class) you have a method that will be used by all other derived classes, but kinda useless to the daddy himself. Now, each derived class(the "kid" classes) will use this method, but depending on the kid, may return/do different things. An example:<BR><BR>Super Class People<BR> method talk(){}; //useless here, but necessary for the others<BR><BR>Now, we have 3 other classes derived from People.<BR><BR>Class Soprano<BR> method talk returns "Fugitaboutit"<BR><BR>Class Simpsons<BR> method talk returns "D&#039;Oh"<BR><BR>Class Eastwood<BR> method talk returns "Go ahead, make my day"<BR><BR><BR>Now your program will use the method talk, but depending on who the class is, will return different things. Does that make sense? (It didn&#039;t to me at first)<BR><BR>I&#039;m not an expert on this topic, so someone else may have a better way of explaining it to you.

  4. #4
    Join Date
    Dec 1969

    Default Not really available in VBScript...

    VBScript doesn&#039;t provide for inheritance of classes, so true polymorphism isn&#039;t available.<BR><BR>And JavaScript only handles it in somewhat funky way.<BR><BR>You really need a language that allows inherited classes (e.g., Java or C++) to get a good feel for how powerful it is.<BR><BR>On the other hand, you *can* "fake it" in VBS.<BR><BR>As the other poster said, the principal is that all classes in a "tree" of classes will always implement all the methods of any class "above" them in the tree. So that if you have an "Animal" class that implements maybe "Order" and "Species" and "Size" methods, then every class that is a sub-class of "Animal" will implement those same methods, but providing their own implementation instead of the default one that is provided by "Animal".<BR><BR>Okay, so it&#039;s not inheritance, but you *can* do the same thing in VBScript classes. Just be sure that you "Elephant" class implements all the methods that "Animal" does and now it won&#039;t matter to some other chunk of code whether the *actual* object it is handed is an "Elephant" or an "Animal", it can always count on being able to call the "Size" method. Note that it is perfectly legal to add other, NON-polymorphic, methods to a class. So if "Elephant" adds a "Trumpet" method and then some chunk of code calls myAnimal.Trumpet() and it turns out that the myAnimal variable is actually an instance of "Worm"...well, that&#039;s okay...you should only call the methods that you *KNOW* are implemented by all classes in the class "tree."<BR><BR>And therein lies the beauty of Java and C++. Those languages ENFORCE AUTOMATICALLY the rules of polymorphism. You can&#039;t possibly create a class "Elephant" the is a sub-class of "Animal" in Java or C++ that does *not* implement all the methods of "Animal". And that&#039;s why people like those languages for writing OO applications. Simple as that!<BR><BR>

  5. #5
    RBliss Guest

    Default Thank you everyone!

    A lot of information to digest! Thank you all for your time.

  6. #6
    RBliss Guest

    Default One more question

    Then what you have is the same as overriding the method talk, is it not? I thought I had it, then I thought of this question.

  7. #7
    Join Date
    Dec 1969

    Default And when you finish

    you need to cover malapropisims. There will be a quiz.

  8. #8
    Join Date
    Dec 1969

    Default Yes and no...

    It depends on a lot of things. For example, in C++ code, a method is only Polymorphic from the point in the tree at which it is declared "virtual" and downward. In Java, a method is always virtual.<BR><BR>EXCEPT...<BR><BR>Except if you change the "signature" of the method.<BR><BR>For example, if the parent class had (C++ notation):<BR><BR>virtual char * talk( ) { .... }<BR><BR>and the subclass used<BR><BR>virtual char * talk( int i, char * c ) { ... }<BR><BR>then the subclass talk(int,char) is *not* polymorphic with the base class&#039;s talk(). (In C++, the return type of the method does not enter into the "signature" of the method, but I ignored that for simplicity here and kept them the same.)<BR><BR>See, the other point about polymorphism in languages with true inheritance is "Type Safety".<BR><BR>If you have (now in Java) classes such as these:<BR><BR>public class Animal<BR>{<BR>&nbsp; &nbsp; String classification( ) ...<BR>}<BR>public class Elephant extends Animal<BR>{<BR>&nbsp; &nbsp; String classification( ) ...<BR>}<BR>public class Parrot extends Animal<BR>{<BR>&nbsp; &nbsp; String classification( ) ...<BR>}<BR><BR>And then, in some other chunk of code, you do this:<BR><BR>...<BR>Animal myAnimal = someFunctionThatAlwaysReturnsAnElephant( );<BR>String whatever = myAnimal.classification( );<BR>...<BR><BR>That code is GUARANTEED to work, and the compiler KNOWS at compile time that it will! You will never get a runtime error from it (well, unless the methods themselves fail, of course).<BR><BR>On the other hand:<BR><BR>...<BR>Elephant myElephant = someFunctionThatReturnsAnAnimal( );<BR>...<BR><BR>That code CAN NOT BE CHECKED at compile time! *Maybe* the returned object is an Elephant, maybe it isn&#039;t. And Java will choke and complain and insist on a "cast":<BR><BR>...<BR>Elephant myElephant = (Elephant) someFunctionThatReturnsAnAnimal( );<BR>...<BR><BR>which will get you past the compiler but *still* might give an Illegal Cast exception when you run the code if it is *not* an Elephant that is returned.<BR><BR>Where the heck was I going with this?<BR><BR>Oh, yes... The point being that if you declare the data type of your variable as referencing something *higher* in the class tree, then it is *always* compile-time-legal to call a method/function that returns an instance of any class from that higher point on "down" in the tree!<BR><BR>Again, this is really powerful, because you *know* (without having to trap run-time errors) that the assignment and subsequent method calls will all compile and work. <BR><BR>In VBScript (and other languages) you have no such assurance: Without the class heirarchy to draw upon, the compiler cannot possibly know whether or not a particular assignment or particular method call will work, so you *must* be ready to do run-time error handling (or code awfully damned carefully!). Yeah, you can make it work, but it somehow just isn&#039;t as "clean".<BR><BR>Whew. That&#039;s a long enough book for now!<BR><BR><BR>

  9. #9
    The Old Fart Guest

    Default What, no spoonerisms? <eom>


Posting Permissions

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