views:

105

answers:

2

Hey

Im trying to use generic types for the first time in Java , as I only want my constructor to accept classes that implement the "Anealable" interface. theres a problem with my code by the only error I get is "Illegal start of Type" which is not getting very far with trying to make it work

here is the code for my class

package simulated_anealing;

public class Crystal extends Thread {

    Object a;

    public  Crystal(<? implements Anealable> a)
    {
        this.a = a;
    }

}
+6  A: 

Why don't you just pass into the constructor the Anealable type like this :

package simulated_anealing;

public class Crystal extends Thread {

    Object a;

    public  Crystal(Anealable a)
    {
        this.a = a;
    }

}
Amir Afghani
Will this mean that construtor will now accept only Classes that implement the Anealable interface?
Gwilym
ok just had a play with my code and this seems to work out. thanks
Gwilym
+3  A: 

I assume you want to parameterize the class. If so then:

public class Crystal<T extends Anealable> extends Thread {
  Object a;

  public  Crystal(T a) {
    this.a = a;
  }
}

Alternatively, can parameterize methods (including constructors) like so:

public class Crystal extends Thread {
  public <T extends Anealable> Crystal(T t) {
    // do stuff with T
  }
}

It's hard to determine what your intent is so I can't really comment on which would suit your needs.

cletus
You *can* parameterize constructors too.
erickson
@erickson: quite right, cheers, fixed.
cletus