Loading ...
Sorry, an error occurred while loading the content.

Polymorphism question

Expand Messages
  • arturuong
    I m trying to determine if the RoboJDE compiler is particular about polymorphism or I m just missing something. I m guessing it s the latter. Basically what
    Message 1 of 3 , Feb 20, 2007
    • 0 Attachment
      I'm trying to determine if the RoboJDE compiler is particular about
      polymorphism or I'm just missing something. I'm guessing it's the
      latter. Basically what is the difference between

      A.
      SeaCreature creatures = new SeaCreature[2];
      creatures[0] = new Fish(...);
      creatures[1] = new Mermaid(...);
      ...
      creature[currentCreature].swim();

      Fish and Mermaid extend SeaCreature;


      B.
      Thread siren[] ;
      siren = new Thread[] { new SirenLight(),
      new SirenSound()};
      siren[0].toggle();

      SirenLight and SirenSound extend Thread

      Example A is ok, example B yeilds the following error...


      Found 2 semantic errors compiling "D:/MyDocs/Saint/Robot/Siren.java":
      11. siren[0].toggle();
      ^---------------^
      *** Semantic Error: No method named "toggle" was found in
      type "java.lang.Thread".


      I think I know what the problem may be but I want to hear but people
      say.

      Thanks
      Arturo
    • Don French
      The problem is exactly what the error message told you. Thread has no method named toggle. ... From: arturuong To: intellibrain@yahoogroups.com Sent: Tuesday,
      Message 2 of 3 , Feb 21, 2007
      • 0 Attachment
        The problem is exactly what the error message told you.  Thread has no method named toggle. 

         
         
         
        ----- Original Message -----
        From: arturuong
        Sent: Tuesday, February 20, 2007 9:55 PM
        Subject: [intellibrain] Polymorphism question

        I'm trying to determine if the RoboJDE compiler is particular about
        polymorphism or I'm just missing something. I'm guessing it's the
        latter. Basically what is the difference between

        A.
        SeaCreature creatures = new SeaCreature[ 2];
        creatures[0] = new Fish(...);
        creatures[1] = new Mermaid(...) ;
        ...
        creature[currentCre ature].swim( );

        Fish and Mermaid extend SeaCreature;

        B.
        Thread siren[] ;
        siren = new Thread[] { new SirenLight() ,
        new SirenSound() };
        siren[0].toggle( );

        SirenLight and SirenSound extend Thread

        Example A is ok, example B yeilds the following error...

        Found 2 semantic errors compiling "D:/MyDocs/Saint/ Robot/Siren. java":
        11. siren[0].toggle( );
        ^----------- ----^
        *** Semantic Error: No method named "toggle" was found in
        type "java.lang.Thread" .

        I think I know what the problem may be but I want to hear but people
        say.

        Thanks
        Arturo

      • David Stampf
        To be a bit wordier, SeaCreature has the swim method, all of the classes that extend it will have the swim method. Thread does not have a toggle. All of your
        Message 3 of 3 , Feb 22, 2007
        • 0 Attachment
          To be a bit wordier,

          SeaCreature has the swim method, all of the classes that extend it will have the swim method.
          Thread does not have a toggle. All of your classes that have extended Thread have a toggle method, but that does not work it's way upward to the parent class.

          You need to create a base class (or interface) that has a toggle method and which either implements the Runnable interface or has-a Thread - lets say this is named X. Then you can have SirenLight and SirenSound extend/implement X and then what you are trying to do will work.

          My advice is to learn how to use interfaces. Unless you have a lot of experience with OO programming, and have a good overall design in mind, you will come to much less grief.

          Dave


          On 2/21/07, arturuong <agarcia@...> wrote:
          I'm trying to determine if the RoboJDE compiler is particular about
          polymorphism or I'm just missing something.  I'm guessing it's the
          latter.  Basically what is the difference between

          A.
               SeaCreature creatures = new SeaCreature[2];
               creatures[0] = new Fish(...);
               creatures[1] = new Mermaid(...);
               ...
               creature[currentCreature].swim();

          Fish and Mermaid extend SeaCreature;


          B.
               Thread siren[] ;
               siren = new Thread[] { new SirenLight(),
                                  new SirenSound()};
               siren[0].toggle();

          SirenLight and SirenSound extend Thread

          Example A is ok, example B yeilds the following error...


          Found 2 semantic errors compiling "D:/MyDocs/Saint/Robot/Siren.java":
              11.                 siren[0].toggle();
                                  ^---------------^
          *** Semantic Error: No method named "toggle" was found in
          type "java.lang.Thread".


          I think I know what the problem may be but I want to hear but people
          say.

          Thanks
          Arturo




          Yahoo! Groups Links

          <*> To visit your group on the web, go to:
              http://groups.yahoo.com/group/intellibrain/

          <*> Your email settings:
              Individual Email | Traditional

          <*> To change settings online go to:
              http://groups.yahoo.com/group/intellibrain/join
              (Yahoo! ID required)

          <*> To change settings via email:
              mailto:intellibrain-digest@yahoogroups.com
              mailto: intellibrain-fullfeatured@yahoogroups.com

          <*> To unsubscribe from this group, send an email to:
              intellibrain-unsubscribe@yahoogroups.com

          <*> Your use of Yahoo! Groups is subject to:
              http://docs.yahoo.com/info/terms/


        Your message has been successfully submitted and would be delivered to recipients shortly.