I am creating an interface and an implementation of linked list like so in Java 1.8:
public interface MyList<E extends Comparable<E>> {
.....
}
public class MyListImpl<E> implements MyList<E extends Comparable<E>>{
......
}
The interface has no compiler issues but the MyListImpl is giving an error Unexpected Bound where I have E extends Comparable<E>>. I am not sure why this error is happening though.
The bounds always go on the declaration of the generic type. That's the one at the end of the class name. The one in the implements clause is using the already declared type. So: public class MyListImpl<E extends Comparable<E>> implements MyList<E>.
If you want to make it a bit better, use <E extends Comparable<? super E>>. That looks the same but isn't; with just Comparable<E> you won't be able to support classes like java.sql.Timestamp, which extends java.util.Date which implements Comparable<Date>. In other words: Timestamp is not Comparable<Timestamp> but Comparable<Date>, and using Comparable<? super E> will allow you to use it.
Related
I'm creating a generic interface and its class implementation. It's a disjoint set. The generic type upper boundary for this class is generic itself:
public class MyDisjointSet<K extends Pair<T, T>> implements IMyDisjointSet<K> {
}
and
public interface IMyDisjointSet<T> {}
but I get compile error Cannot resolve symbol T.
When I add a second parameter to the list of generic types it works compiles:
public class MyDisjointSet<K extends Pair<T, T>, T> implements IMyDisjointSet<K>
Why should we put T here? why not before class name?
Please try
public class MyDisjointSet<K extends Pair<T, T>,T> implements IMyDisjointSet<K> {
I want make my List. But I dont know how to write generic type in java.
public interface myListInt <E extends Comparable<E>>{}
public class myList<E extends myListInt<E>> extends LinkedList{}
When I am doing that, it gives an error.How should ı write.
The exact intent of your code is unclear, but I got an error for the <E> in myListInt<E>.
public interface myListInt <E extends Comparable<E>>{}
public class myList<E extends myListInt<E>> extends LinkedList{}
^ Error here
This is because you need to constrain E to extend Comparable<E> in order to be a valid bound for myListInt<E>:
public class myList<E extends Comparable<E> & myListInt<E>>
extends LinkedList{}
However, you maybe also want to add a constraint to LinkedList too (assuming this is java.util.LinkedList:
public class myList<E extends Comparable<E> & myListInt<E>>
extends LinkedList<E> {}
I am Studying Data Structures and I was asked to write a program that allows a store manager to manipulate an inventory , using 4 classes : ListInterface , ExpandableArrayList ( a class that implements the interface) , class item ( which is the type stored in the Arraylist) , and of course, a Test class.
some methods require the use of compareTo. that is , being Comparable , but I don't know what class exactly should be Comparable ?
and if I should write Implements or extends Comparable ?
these are the class headers I have right now :
public interface ListInterface<T extends Comparable <?super T >> {... }
public class ExpandableArrayList <T extends Comparable <? super T >>
implements ListInterface <T> { ...... }
public class Item<T extends Comparable<T>> {... }
but for some reason, I cannot create an Object in the Test class.
when I type:
ListInterface<Item> inventoryList= new ExpandableArrayList<Item>();
I get the following errors :
Test.java:9: error: type argument Item is not within bounds of type-variable T
ListInterface<Item> inventoryList= new ExpandableArrayList<Item> () ;
where T is a type-variable:
T extends Comparable<? super T> declared in interface ListInterface
Test.java:9: error: type argument Item is not within bounds of type-variable T
ListInterface<Item> inventoryList= new ExpandableArrayList<Item> () ;
where T is a type-variable:
T extends Comparable<? super T> declared in class ExpandableArrayList
How Can I solve this? what exactly should be changed? ..
thanks a lot in advance.
T which is your type Item needs to implement Comparable. This will allow the ExpandableArrayList class to run the compareTo method on elements of type Item using the comparison provided by that class. When you implement compareTo from Comparable you have to give a way for comparing different Items, this should be based on the attributes of the Item class ideally.
public class Item implements Comparable<Item> {... }
This is what the class def would look like.
You have to write implements for interfaces and extends for classes.
Inheritance tutorial
Interfaces tutorial
It should be like this -
public interface ListInterface<T extends Comparable<? super T>> {... }
public class ExpandableArrayList<T extends Comparable<? super T>> implements ListInterface <T extends Comparable<? super T>> { ...... }
public class Item implements Comparable<Item> {... }
I have a very specific problem with java generics. The follwowing classes and interfaces have been predefined:
public interface IFirst<R, T> {...}
public abstract class AbstractFirst<T extends AbstractFirst, L extends IFirst<String, T>> {...}
public interface ISecond extends IFirst<String, AbstractSecond> {...}
public abstract class AbstractSecond extends AbstractFirst<AbstractSecond, ISecond> {...}
Now I've created a following repo definition which seems to be valid:
public abstract class AbstractRepo<T extends AbstractFirst<T, IFirst<String,T>>> {...}
But now that i want to extend it:
public class RepoFirst extends AbstractRepo<AbstractSecond> {...}
I get the following error:
Bound mismatch: The type AbstractSecond is not a valid substitute for the bounded parameter
<T extends AbstractFirst<T,IFirst<String,T>>> of the type AbstractRepo<T>
I cannot change the first four (at least not radically) beacuse they are too heavily ingrained with the rest of the application, but the second two are new and up for change if need be.
Also intrestingly it allows the following (with raw type warnings):
public class RepoFirst extends AbstractRepo {
...
#Override
AbstractFirst someAbstractMethod() {
return new AbstractSecond() {...};
}
...
}
But for code clarity I would like to implement it with clearly defining AbstractSecond as the generic type for Abstract Repo.
What am I missing?
Your AbstractRepo expects an instance of IFirst and not a subtype of IFirst. But your AbstractSecond is clearly not IFirst. (I mean it is, from a OO standpoint but for generics, List<Number> is not the same as List<Integer>). It's ISecond. It might work if you could change your AbstractRepo from IFirst to ? extends IFirst as you did for AbstractFirst.
My main question revolves around when to use and what is the difference between the following when combined with Class, Abstract, Interface:
<E>
<E extends Interface>
<? extends Interface>
Shown below is a detailed question with some code signatures:
This code uses Guava Forwarding Decorators to define specific collections.
Base Interface:
public interface AnimalSetInterface<E extends AnimalI> extends Set<E>
This works:
public interface AsiaI<E extends AnimalI> extends AnimalSetInterface<E>
The following gives an error:
public interface AsiaI<E> extends AnimalSetInterface<E>
Bound mismatch: The type E is not a valid substitute for the bounded
parameter of the type AnimalSetInterface
What I am trying to understand is if I have specified at the Base Interface that I only want <E extends AnimalI> then why do I have to specify again in AsiaI?
I am trying to understand generics and at the same time minimize code.
Also if both classes have such code is there a good way to combine/minimize (remove/generify boilerplate code) it:
Asia:
public Asia(final ImmutableSet<E> animalSet){
super(animalSet);
}
public static <E extends AnimalI> AsiaI<E> of(final ImmutableSet<E> animalSet){
return new Asia(animalSet);
}
Africa:
public Africa(final ImmutableSet<E> animalSet){
super(animalSet);
}
public static <E extends AnimalI> AfricaI<E> of(final ImmutableSet<E> animalSet){
return new Africa(animalSet);
}
public class Africa<E extends AnimalI> extends AnimalSetAbstract implements AfricaI
public class Asia<E> extends AnimalSetAbstract implements AsiaI
The difference is that in the first case, your generic type must extend AnimalI
In the second case, your generic type could be any class.