what does this interface declaration mean in java? - java

It might be a noob question but I am learning java and I came across an interface which had its definition like :
public interface MyClass <T extends Comparable<T>>
Can someone please explain what does it mean? I mean what kind of interface is created.

It means that the generic type argument must implement the Comparable interface.
When specifying <T extends Comparable<T>> you can use e.g. Collections.sort in this interface on type T. Without extends you can not guarantee that T is comparable.
Numbers and String are e.g. comparable and implement the Comparable interface.

The interface takes a type T which is Comparable with other T.
The interface is much the same only its generic type is constrained.

It means that T must implement Comparable over the same object.
for example
public interface MyClass <T extends Comparable<T>>
en then you can use as follow
public class MyImpl implements MyClass<String>
It is valid because String implements Comparable. But the following sentence is not valid because MyNewImpl is not implementing Comparable.
public class MyNewImpl {}
public class MyImplTwo implements MyClass<MyNewImpl>
Regards
Ignacio

Related

How to restrict generic type of interface to accept only implementor class

My case is different but as an example
public interface Comparable<T>
would allow me to say:
public class MyType implements Comparable<OtherType>
but this is rarely what you want to achieve.
Is there a way to force me to say:
public class MyType implements Comparable<MyType>
The closest I got is:
public interface Comparable<T extends Comparable<T>>
This only works partially, as it won't allow OtherType if it's not comparable itself, but would allow:
public class MyType implements Comparable<Integer>
as it meets the conditions.
This is not possible in Java.
Consider if it were possible to require that the type argument of Comparable be the same as the implementing class. Then if you had a class Foo implements Comparable<Foo>, and then also a class Bar extends Foo, Bar would also automatically implement Comparable<Foo> by the way that inheritance in Java works. But that would violate the constraint that the implementing class is the same as the type argument, as Bar does not implement Comparable<Bar> (and you can't even explicitly have Bar implement Comparable<Bar>, as a class cannot implement a generic type with two different type arguments).

How do I declare a class that implements an interface that constrains the type of a class?

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

Declaring an object that implements an interface

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;

Generic type defined as subclass + interface

I need to defined a Generic type as a subclass implementing an interface, like this (DOES NOT COMPILE):
public class Foo<T extends SomeClass implements SomeInterface> {
...
}
Is it possible to do something like this?
Jon Skeet, where are you when we need you ;)
That's the syntax:
public class Foo<T extends SomeClass & SomeInterface> {
}
These are called Intersection Types. They don't differentiate between classes and interfaces. Intersection types are also briefly mentioned in the Java tutorial on Bounded Type Parameters.

Extending both T and SomeInterface<T> in Java

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 ...

Categories