I'm trying to declare an object which must implement a specific interface.
I thought the following would work in Java as it does in some other languages but I'm at a loss here:
Class<? implements **theInterface**> implementingObject
Any pointers would be appreciated
for generics, you use "extends" regardless of whether it is a Class or Interface.
Class<? extends **theInterface**> implementingObject
What you are declaring here isn't an object that implements an interface, but a class of an object which implements that interface. An object implementing an interface is simply declared as the interface type, i.e.
theInterface implementingObject;
Related
I've seen examples of how to declare a generic class whose type parameter is constrained, e.g. it must extend Comparable. I also know how to define an interface for a generic class whose type is similarly constrained. However, when I do the latter, I'm unable to figure out the syntax for the class signature. So, to cut a long story short, if the interface is
public interface iMyClass<T extends Comparable<T>>
what should be the syntax for the signature for the implementing class
public class MyClass.......
Thanks in advance,
D
Do you intend MyClass to be generic as well? If so,
public class MyClass<? extends Comparable<T>> implements iMyClass<T> {
Otherwise, keep things simple and
public class MyClass implements iMyClass<String> {
Replace String with your Comparable.
A constrained type argument is supplied in exactly the same way as a non-constrained one, for instance:
public class MyClass implements iMyClass<String> {
This question is a follow up from Java Generics Wildcarding With Multiple Classes.
I'm writing an interface like this:
public interface SomeInterface {
public Class<? extends SomeClass implements OtherInterface> getClassForObject(Object object);
}
I know that this is wrong syntax, as is Class<? extends SomeClass & OtherInterface>, and it seems all options.
I can't have the interface do what was suggested in the answer to the question linked above (public interface SomeInterface<T extends SomeClass & OtherInterface>) because implementations of this interface might want to return different things for different inputs.
I also can't create an abstract class that extends SomeClass and implements OtherInterface and have everything extend from that because there are many existing implementations of SomeClass that clients may want to extend from.
Is there any way to force implementations of this interface to return a type that fits both constraints? Besides throwing a runtime exception somewhere else in the code?
You cannot impose multiple restrictions on a wildcard. You can declare a generic type parameter on the method instead of the interface itself, and you can impose multiple restrictions on it. Try:
public interface SomeInterface {
public <T extends SomeClass & OtherInterface> Class<T>
getClassForObject(Object object);
}
I don't think this is a duplicate of Check if a generic T implements an interface, but it may be(??).
So, I want to create a generic interface that only allows objects that implements two interfaces. Alike is a costum interface.
public interface AbstractSortedSimpleList<T extends Comparable<T>, Alike> {}
If I understand it correctly, Java now tries to create a generic interface AbstractSortedSimpleList<T,Alike>, which isnt exactly what I want to achieve. I want AbstractSortedSimpleList<T> where T has to implement both Comparable<T> and Alike.
Later, I want to make a new class
public class SortedSimpleList<T> implements AbstractSortedSimpleList<T> {}
The point here is to create a class SortedSimpleList<T> where T has to be implementing the aforementioned interfaces. But my code does not seem to work very well.
You can give multiple bounds to type parameter:
public interface AbstractSortedSimpleList<T extends Comparable<T> & Alike>
Then, your SortedSimpleList would be like:
class SortedSimpleList<T extends Comparable<T> & Alike> implements AbstractSortedSimpleList<T> {}
See JLS ยง4.4:
Every type variable declared as a type parameter has a bound. If no bound is declared for a type variable, Object is assumed. If a bound is declared, it consists of either:
a single type variable T, or
a class or interface type T possibly followed by interface types I1 & ... & In.
Note:
You can't have such multiple bounds for wildcards though. It's only for type parameters.
References:
Java Generics FAQs - Type Parameter bounds
Use some generic bounds with the & notation
interface AbstractSortedSimpleList<T extends Comparable<T> & Alike> {
See the official Java tutorial on multiple bounds, here.
I've got a class Foo<T>. How can I say that I want T to be some class implementing BarInterface? Writing simply class Foo<T implements BarInterface> doesn't compile.
Use extends instead of implements.
I want to create a class that takes two parameters. One should be typed simply as T. The other should be typed as something that extends both T and SomeInterface<T>. When I attempt this with
public class SomeClass<T, S extends SomeInterface<T> & T>
then Java complains with
"The type T is not an interface; it cannot be specified as a bounded parameter"
and if instead I attempt to create an interface for S with
public interface TandSomeInterface<T> extends SomeInterface<T>, T
then Java complains with
"Cannot refer to the type parameter T as a supertype"
Is there any way to do this in Java? I think you can do it in C++...?
You can't create an interface that extends the type parameter T since there's no contract that would guarantee T to be an interface. And of course interface extending a class is not allowed.
this works if you extend an interface as well:
public class SomeClass<T extends I, S extends SomeInterface<T> & I>
but maybe it's not exactly what you want ...