Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java Generic Type flexibility with subtype assignment

I have the following structure. A parent class:

public class MyTree<E extends MyTree<E>>{
   List<E> children;

   public MyTree(List<E> ls){
      this.children = ls;
   }
   
   public void insert(E t){
     this.children.add(t);
   }
}

a child class:

public class SpecTree<E> extends MyTree<SpecTree<E>>{
   private E value;

   public SpecTree(E value){
      super(new ArrayList<>());
      this.value = value;
   }
}

Now from main, I want to insert a tree into another tree.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SpecTree<Number> st = new SpecTree<>(0);
st.insert(new SpecTree<Integer>(2));      //incompatible type error

The SpecTree must be able to accept new children with values that are subtype of the current tree. For example, I should be able to insert a SpecTree<Integer> into SpecTree<Number>.

Is it possible to do without changing the structure of code and changing only the type parameters?

>Solution :

The error occurs because insert expects SpecTree<Number>, but we are giving it SpecTree<Integer>.

According to PECS, insert would be able to take both SpecTree<Number> and SpecTree<Integer> if it had took a SpecTree<? extends Number>. A producer of integers is a consumer of numbers.

That means changing E to SpecTree<? extends E>:

public void insert(E t)

Therefore:

class SpecTree<E> extends MyTree<SpecTree<? extends E>>

However, now the type argument SpecTree<? extends E> does not fit the bound specified in MyTree, which is E extends MyTree<T>, so we change that too:

class MyTree<E extends MyTree<? extends E>>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading