Implementing a Generic Interface In Java - 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)

Related

Java Generics: Foo<T>, Foo, Foobar<T extends Foo<T>> and Foobar<T extends Foo>

In Java Generics, given a generic class/interface Foo<T>,
What's the difference between declaring a new generic class:
Foobar<T extends Foo<T>> or simply Foobar<T extends Foo>, also why can I instantiate a generic class Foo<T> without instantiating the type parameter T?, i.e. why can i write the following:
Foo var = new Foo();, does this mean that the class is instantiated with an object, through which i can only use the non-generic method?
please forgive me if the question is not so clear, the example i was working on is the following:
MyClass<T extends Comparable<T>>
class Foo<T> {}
is your class.
Foo yourVariable = new Foo();
equals Foo<Object> yourFoo = new Foo<Object>();
class Foobar<T> extends Foo {}
equals class Foobar<T> extends Foo<Object> {}
the answer to your question
class YourClass<T extends Comparable<T>> {}
means YourClass's type T is able to compare itself to objects of T (its class), whereas
class YourClass<T extends Comparable> {}
's type T is able to compare itself to objects of class Object, which is not what you want

How make a generic type in java?

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> {}

Correctly extend interface with generics

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> {
}

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