So I'm creating an implementation of a priority queue using generics. I have this interface which I am trying to implement in my PriorityQueue class:
public interface PriorityQueueInterface<Item extends Comparable<Item>> { }
but I'm not sure what the proper syntax is to correctly implement the PriorityQueueInterface. Here is what I currently have:
public class PriorityQueue<Item extends Comparable<Item>> implements PriorityQueueInterface<Item extends Comparable<Item>>{ }
but it's throwing multiple errors. What would be the correct way to implement the interface? Any help would be appreciated.
You've already declared Item to be Comparable<Item> with the class definition of PriorityQueue. You only need to reference it in the implements clause, where you don't need to repeat that it's Comparable<Item>. You reference a generic type parameter in the implements or extends clause just as you would for any other part of the class body where the generic type parameter is in scope.
Try
public class PriorityQueue<Item extends Comparable<Item>>
implements PriorityQueueInterface<Item>{ /* implement here */ }
Related
So lets take this example that I have a class A and class B both A and B are implementing a specific interface letters. Now I need to make a specific function in another class wherein I need to pass either A class object or B class object and similarly do some operation on these objects and return either A or B class objects. Now I can define the function using a generic type T but the catch is T must always implement interface letters.
So a Object of class 1 which doesn't implement interface letters wont be allowed to pass this function.
public class A implements letters{...}
public class B implements letters{...}
public class LetterOperations{
public T letterOp(T letter){..}
}
Here letterOp() must be accepting only those kind of generic classes T which implement interface letters.
Add type parameter bound when declaring generic class:
public class LetterOperations<T extends letter> {
public T letterOp(T letter){..}
}
Or use method with type parameter:
public <T extends letter> T letterOp(T letter){..}
I want to implement a super class and it is a java.util.concurrent.Callable . The child class will return different types of objects for Callable. Therefore, I want set this super class callable's generic type as to be any type. Following is my super class declaration.
public abstract class AbsTaskRunner implements java.util.concurrent.Callable {
I want to set generic type of Callable as any type.
public abstract class AbsTaskRunner implements java.util.concurrent.Callable <HERE_COMMON_TYPE> {
You can try with:
public abstract class AbsTaskRunner<T> implements java.util.concurrent.Callable<T>
Then the sub-classes can be either generic (like this):
public class GenericSubclass<T> extends AbsTaskRunner<T>
or extend AbsTaskRunner for some specific type (like this):
public class StringSubclass extends AbsTaskRunner<String>
I'm having difficulty using generics for a redesign/refactoring I'm doing on an existing design.
public interface DataDto {
// some data here
}
public interface SetDto<MyDataDto extends DataDto> {
List<MyDataDto> getData();
}
public interface Results<MySetDto extends SetDto<DataDto>> {
MySetDto getResults();
}
public interface MyProblemInterface<MyDataDto extends DataDto,
MySetDto extends SetDto<MyDataDto>,
MyResults extends Results<MySetDto>> {
// some stuff here
}
My problem is that I get the following error for MyProblemInterface:
Bound mismatch: The type MySetDto is not a valid substitute for the
bounded parameter <MySetDto extends SetDto<DataDto>> of the type
Results<MySetDto>
I admit my experience with generics is somewhat limited, but basically I'm trying to enforce that all three of the types in MyProblemInterface are the same "type". For example, if I have ADataDto, BDataDto, ASetDto<ADataDto>, BSetDto<BDataDto>, AResults<ASetDto>, BResults<BSetDto>, I want to ensure a class can't implement MyProblemInterface in a manner like AMyProblemInterface<ADataDto, ASetDto, BResults>. I would think that since MySetDto extends SetDto<MyDataDto> just fine, I could continue to take that further, but I'm apparently wrong.
Thank you for any help.
You want too much from Java generics.
It would be simpler to declare your interface as following:
public interface MyProblemInterface<MyDataDto extends DataDto>
And then force method to use SetDto<MyDataDto> and Results<MySetDto>.
By using generics in class/interface declaration you specify some kind of variety which is determined later in definition. But in your case you said that SetDto and Results will always have MyDataDto as parameter, so there is no variety.
Shouldn't it be something like this instead, and you add the actual classes only when implementing the interfaces.
Updated the code, because I forgot to add the right Results definition. This should work.
public interface DataDto {
// some data here
}
public interface SetDto<T extends DataDto> {
List<T> getData();
}
public interface Results<T extends SetDto<? extends DataDto>> {
T getResults();
}
public interface MyProblemInterface<T extends DataDto, E extends SetDto<T>, K extends Results<E>> {
// some stuff here
}
I am a C# programmer and have agreed to help a fried doing Java homework.
In one example I want to create a class that extends a generic List. In C# this looks like
public class MyListClass : List<MyCustomType>
I have tried
public class MyListClass extends List<MyCustomType>
and get the error "no interface expected here". Well, I am not trying to use an interface... Any hints?
java.util.List is a interface. You need to implement it not extend it.
public class MyListClass implements List<MyCustomType>{
}
You can't extend interface. You must implement it.
But you can extend one of implementations (LinkedList for example):
public class MyListClass extends LinkedList<MyCustomType> {
Java ain't C++, so forget all about standard templates.
What you probably want is just a typed List:
List<MyCustomType> myList = new ArrayList<MyCustomType>();
and that's all.
It would be unusual to have "extending a generic class" as a goal for an assignment. It is unusual in the real world too.
You need to declare a generic class / interface
public class MyListClass<T> implements List<T> { }
Or
public interface MyListInterface<T> extends List<T> { }
Or best of all
public class MyListClass<T> extends AbstractList<T> implements List<T> { }
To be brief, I have:
public interface Dictionary<E extends Comparable<E>> extends Iterable<E> {
And
public class TreeDictionary implements Dictionary<TreeDictionary> {
When compiled, caused an error
TreeDictionary.java:4: type parameter TreeDictionary is not within its
bound public class TreeDictionary implements
Dictionary {
I guess the reason is probably because I'm not fully clear with the declaration of the Dictionary interface.
Would really appreciate if somebody could explain me this :)
public interface Dictionary<E extends Comparable<E>>
That says that whichever type you want to use for E, it must implement the Comparable interface.
Given yourTreeDictionary definition:
public class TreeDictionary implements Dictionary<TreeDictionary>
TreeDictionary does not implement Comparable, and so cannot be substituted for E in the generic type of Dictionary.
Try this instead:
public class TreeDictionary implements Dictionary<TreeDictionary>, Comparable<TreeDictionary>
TreeDictionary should now conform to the constraints of E.
Your TreeDictionary class should also implement the Comaparable interface.
You need implement Comparable interface for it to work
public class TreeDictionary implements Dictionary, Comparable
hope it helps