Correctly extend interface with generics - java

I would like to correctly extend an interface but since is uses generics, I became a bit confused.
Here is the first interface
public interface A<T extends Resource>{
...
}
I would like to write something like this but it does not work.
public interface B<T extends Resource> extends A<T extends Resource>
{
...
}
I do not understand why this does not work.
Could you explain?

Since you have already defined the type T, it should be:
public interface B<T extends Resource> extends A<T> {
}

Related

Implementing bounded generic with java

I have an interface
public interface TransferObjectUtil<B extends BusinessObject,T> {
public T to(B domain);
public B from(T transferObject);
}
I am implementing the class for this as
public class ReflectionBasedTransferObjectUtil<B extends BusinessObject, T> implements
**TransferObjectUtil<B extends BusinessObject, T>** {
For the portion within the ** in the above line the compiler complains that for B extends BusinessObject is not allowed. Why would it be so ?
I am just starting with generics, so pardon me for my novice question.
Your location for the generics is a bit wrong / overeager, try it like this
public class ReflectionBasedTransferObjectUtil<B extends BusinessObject, T> implements TransferObjectUtil<B, T> {

Implementing a Generic Interface In Java

I am new to Java Generics. I have to implement an interface which is of generic type. The syntax is as follows:
public interface A{}
public interface B<T extends A>{
public T methodB(T a) ;
}
Now I have to implement B so Lets say my Class is C
public class C implements B<T extends A>{}
The java compiler is not letting me use it this way. Also I do not want to use raw types. Please help.
It should be
public class C<T extends A> implements B<T>
The type parameter is declared following the class name, and later can be used in the implements clause.
If your implementing class is still a generic type you have to use this syntax:
public class C<T extends A> implements B<T> {}
As explained by Eran.
If C is not generic you simply need to specify the type argument for your interface:
public class C implements B<TypeExtendingA> {}
Where TypeExtendingA implements or extends A (or is A)

Java wildcards with super

I read that using a wildcard with super like this:
public class MyClass <T extends Comparable<? super T>> {
...
}
instead of:
public class MyClass <T extends Comparable<T>> {
...
}
could make the class 'more generic', but I do not understand why.
Can someone provide some concrete examples?
This way you can supply a class for T, which does not for itself implements Comparable, but inherits from a class implementing Comparable.
E.g.
class Baseclass implements Comparable<Baseclass> {
...
}
class Inherited extends Baseclass {
...
}
With a specification like
public class MyClass <T extends Comparable<? super T>> {
...
}
you can use MyClass<Inherited>, and MyClass<Baseclass>, but with
public class MyClass <T extends Comparable<T>> {
...
}
you can only use MyClass<Baseclass>

Usage of extends in Class, Abstract, Interface combination

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.

Java generics - use same wildcard multiple times

I have a class declaration which uses generics and bounded wildcards:
class Factory<T extends Logic<? extends Entity>,
U extends DAO<? extends Entity>>
{
}
Basically its a generic factory, which takes a logic interface (T) and returns a configured implementation. In order to instantiate the logic, I take a appropriate DAO class implementing the DAO interface (U).
Both interfaces for logic and DAO are generic as well and take the type of the entity to work with as their type parameter. However, I want to constrain that further, so that DAO and Logic not only have a type parameter which extends Entity, but that they extend the same Entity. The result may look similiar to that:
class <X extends Entity> Factory<T extends Logic<X>,
U extends DAO<X>>
{
}
Can I achieve that with java generics?
Yes, you're close. Do it like this:
class Factory<X extends Entity,
T extends Logic<X>,
U extends DAO<X>>
{
}
Alternative
class Factory<T extends Logic<?>,
U extends DAO<?>>
{
// Here, the generic method parameter only requires X
// to be the same bound at method invocation. However,
// you will "lose" that information again when the
// Factory is returned.
public static <X extends Entity,
T extends Logic<X>,
U extends DAO<X>> Factory<T, U> createFactory(T logic, U dao)
{
return new Factory<T, U>(logic, dao);
}
}
Another approach could be to provide a wrapper (although that's not really elegant ;) ):
class Entity{}
interface Logic<T extends Entity> {}
interface DAO<T extends Entity> {}
interface DaoLogic<X extends Entity> {
DAO<X> getDAO();
Logic<X> getLogic();
}
class Factory<T extends DaoLogic<? extends Entity>> {}
Would the following work. X would be the "common" type, where Logic and DAO both would use this type.
public class Factory<X extends Entity, T extends Logic<X>, U extends DAO<X>>
{
}

Categories

Resources