0

I have this code:

public interface IDimension<S extends IDimension<S,T>, T extends Number> extends Comparable<S> {
  T toNumber();

  default T toBaseNumber() {
    return toNumber();
  }

  S fromNumber( T units );

  Class<T> numberType();
}

When I implement IDimension like below(Implementation1), I get 'Type parameter 'TestBaseSample' is not within its bound; should implement 'IDimension<TestBaseSample<Float,Integer>, java.lang.Long>' error:

Implementation1 :

class TestBaseSample<Integer, Float> 
    implements IDimension<TestBaseSample<Float, Integer>, Long> { 
}

I understand why 'Implementation1' gives error, but not able to understand why 'Implementation2' and 'Implementation3' works?

Implementation2:

class TestBaseSample2<Integer, Float> 
    implements IDimension<TestBaseSample2<Float, Float>, Long> {
}

Implementation3 :

class TestBaseSample3<Integer, Float> 
    implements IDimension<TestBaseSample3<Integer, Integer>, Long> {
}
2
  • 3
    Do you realize that you are making Integer and Float into variable type names? eg. class TestBaseSample3<T, S> where T and S are generic types. ideone.com/Zq4AVt for example. Maybe you should change those to T & S so it is clear, or make them concrete. class TestBaseSample3 implements ... Commented Oct 19, 2016 at 18:38
  • 1
    @matt you should turn this into an answer. Commented Oct 19, 2016 at 18:42

2 Answers 2

1

It looks like you are attempting to create a concrete type, but you are actually creating generic parameters and giving them the names of java classes.

Consider your first example TestBaseSample2 if we change the generic parameters to T and S

class TestBaseSample2<T, S> 
    implements IDimension<TestBaseSample2<S, S>, Long> {
}

Now, you should be able to make an instance provided S is a Long and T can be anything else.

Here is a simpler example.

static class OtherList<Integer> extends ArrayList<Integer>{}

public static void main (String[] args) throws java.lang.Exception
{
    OtherList<String> list = new OtherList<>();
    list.add("this");
    System.out.println(list.size());
}
Sign up to request clarification or add additional context in comments.

Comments

0

Remove all types from your implementations (and never name generic parameters using class names, especially classes from the JDK - use single letters):

class TestBaseSample implements IDimension<TestBaseSample, Long> { 
}

class TestBaseSample2 implements IDimension<TestBaseSample2, Long> {
}

class TestBaseSample3 implements IDimension<TestBaseSample3, Long> {
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.