views:

43

answers:

3

I'm getting an error here that says I haven't defined a method, but it is right in the code.

class SubClass<E> extends ExampleClass<E>{
      Comparator<? super E> c;
      E x0;

      SubClass (Comparator<? super E> b, E y){
           this.c = b;
           this.x0 = y;
      }

      ExampleClass<E> addMethod(E x){
           if(c.compare(x, x0) < 0)
               return new OtherSubClassExtendsExampleClass(c, x0, SubClass(c, x)); 
               //this is where I get the error                        ^^^^^^
      }

I did define that Constructor for SubClass, why is it saying that I didn't when I try to pass it in that return statement?

+3  A: 

You probably want new SubClass(c, x) instead of SubClass(c, x). In java you invoke constructor in different way than method: with new keyword.

More on the subject.

Nikita Rybak
yup, that's it (+1)
seanizer
Nope, that's not right. **My** answer is right!!! `;)`
jjnguy
@Justin +1 to you for the sense of humor :)
Nikita Rybak
@Nikita, `+1` for being a good sport.
jjnguy
+1 to you all for getting the SAME right answer :) thanks for the help!
BDubs
+2  A: 

I think you want it to be:

                                       // new is needed to invoke a constructor 
return new OtherSubClassExtendsExampleClass(c, x0, new SubClass(c, x));
jjnguy
+1  A: 

As rightly pointed out by others there is missing new required to invoke constructor.

Whats happening in your case here is that due to missing new your call is treated as method call, And in your class there is not method SubClass(c, x). And Error undefined method is correct in your case as there is no method named SubClass(c, x)

You need to correct same :

return new OtherSubClassExtendsExampleClass(c, x0, new SubClass(c, x));
YoK