getting a "function doesn't exist" from a method in a sub-class

edited December 2017 in How To...

So I have a class super and two sub classes. objects of one of these sub classes are getting added to an ArrayList<super> and at one point I'm calling a run() method for all objects in the arraylist, which is a method each sub class has its own version of. that call is throwing the "function doesn't exist". So is this structure not allowed or is it something else in the code?

Tagged:

Answers

  • edited March 2015

    Your post is in "Questions about Code" category. But you haven't provided any code! X_X
    Nonetheless, super is a Java's reserved keyword and thus can't be used as label of anything! [-X

    https://processing.org/reference/super.html

  • Answer ✓

    Have you tried adding a dummy version of your function to the super class?

  • Answer ✓
    class Super {
      Super() {
      }
      // Add this function...
      void name() {
        println("I'm super, thanks for asking!");
      }
      void time() {
        println("Time passes.");
      }
    }
    
    class Sub extends Super {
      Sub() {
      }
      void name() {
        println("I'm a sub. The sandwich. Yum.");
      }
    }
    
    class Submarine extends Super {
      Submarine() {
      }
      void name() {
        println("I'm a submarine. PING. PING.");
      }
    }
    
    ArrayList<Super> things = new ArrayList();
    
    void setup() {
      size(200, 200);
    
      things.add( new Super() );
      things.add( new Sub() );
      things.add( new Submarine() );
    
      things.get(0).name();
      things.get(0).time();
      things.get(1).name();
      things.get(1).time();
      things.get(2).name();
      things.get(2).time();
    }
    
    void draw() {
      background(0);
    }
    
  • @TFGuy44: thank you so much, specially for the effort of putting the example together! do you happen to know why it's necessary to have that dummy in the superclass? I'd never encountered that issue before, but then again I can't be sure I've put myself in a situation like this before..

    @GoToLoop: I should have just left it in "programming questions"? also, I didn't mean that my actual class was called super, I was just using those terms to explain the situation =P

  • edited March 2015
    • W/o providing some code, most correct category would be "How to...". Just changed that for ya!
    • But remember that we can edit our own posts, including changing category & title!
    • Since I didn't have any code to work with, any possible "bugs" end up being considered.
    • Rather than super, Parent, Base or even Super would be less prone to be mis-interpreted!
  • Answer ✓

    I don't know why it works. I've run into this issue before too myself. It's not really great for learning inheritance. Ideally you wouldn't have to specify it in the base/super class...

  • As said, showing some code reproducing the behavior your describe would help us in diagnosing your issue. You don't have to give all your code, on the contrary. Try and make a couple of dummy classes showing how you do, so we can try on our own and advise.

  • TFGuy44's suggestion fixed it, like I said, but thanks.

  • If your code is similar to TFGuy44's one, then it is clearer: if Super class hasn't the name() function, the problem is that the code gets instances of Super, which can be Sub or Submarine actually. But Super doesn't has a name() method (from what I understand of your problem). If you have never instances of Super, you can make it an abstract class: then you can declare an abstract name() function: no need to give it code, but the code will know all subclasses will have the method.

Sign In or Register to comment.