Java generics extends syntax - java

Can someone please explain me this class definition statement
public class BinarTree<Type extends Comparable<Type>> {...
I completely understand the purpose of it but not the syntax. According to me it should be just
public class BinarTree<Type extends Comparable> {...
What's the meaning of
<Type extends Comparable<Type>> ?
^^^^

Comparable is a generic interface. The reason behind that is to avoid casting to a specific type in the Comparable#compareTo(...) method.
So if a Type extends Comparable<Type> this would mean that the Type will derive a method with signature
public int compareTo(Type t1)
instead of
public int compareTo(Object o1)

The interface Comparable is itself a template.
So what you have there is a template with a parameter that must extend a template.
And specifically it must extend a template that received the extending class as a parameter.
Comparable is a template for interfaces that implement an order relationship and implement the method int compareTo(TYPE o).
So it's normal to define a class:
class FooBar implements Comparable<FooBar> {...
A binary tree wouldn't work for a class that was declared:
class FooBar implements Comparable<Snafu> {...
That's because you would be able to compare FooBars to Snafus but not each other.

In
public class BinarTree<Type extends Comparable>{...
Type can be any Comparable, also Comparable<Integer> or Comparable<OtherType>. If this is what you want, it is fine. Most times, I think you know what you want to compare exactly, so specialize the Comparable to Comparable<Type>

Related

Why use exdend and implement at same time [duplicate]

The full context being:
public class RClass<T extends Comparable<T>>
Would I be right in saying that the statement in the title means that the arguments plugged into the method must either be an object of a class which implements Comparable OR one of its derived classes?
Thanks.
This means that the type parameter must support comparison with other instances of its own type, via the Comparable interface.
An example of such a class is provided in the Oracle tutorial Object Ordering. Note the similar pattern to T extends Comparable<T> in the excerpt below:
public class Name implements Comparable<Name> {
...
public int compareTo(Name n) { ... }
}
Java- The meaning of <T extends Comparable<T>>?
a) Comparable <T>
is a generic interface (remember it's an "interface" i.e not a "class")
b) extends means inheritance from a class or an interface.
From above-said point#a, it is an interface..(Remember it is an inheritance from an "interface" i.e not from a "class")
c)From above-said both points #a & #b,
here "one interface" extends "another interface".
There should be an interface defined for this class..
just an example here is
interface MinMax<T extends Comparable<T>> {
T min();
T max();
}
d) now your class i.e public class RClass {} SHOULD
1# EITHER "implement" this "generic interface" Comparable<T> ..!!!
ex: public class RClass<T> implements Comparable<T>
2# OR create an interface and extend to this "generic interface" Comparable<T>
ex:
interface MinMax<T extends Comparable<T>> {
T min();
T max();
}
class RClass<T extends Comparable<T>> implements MinMax<T> {
.....
.....
}
Here,
Pay special attention to the way that the type parameter T is declared by RClass and
then passed to MinMax. Because MinMax requires a type that implements Comparable,
the implementing class (RClass in this case) must specify the same bound. Furthermore,
once this bound has been established, there is no need to specify it again in the implements clause.
Somewhere in that class, the programmer needs to write something like
if(t.compareTo(othert) < 0) {
...
}
For that to work, the type T must have a compareTo-method which compares it to another object of type T. Extending Comparable guarantees the existence of such a method, among other things.
It means that you can only create an instance of RClass with a type which quite literally extends Comparable<T>. Thus,
RClass<Integer> a;
is acceptable, since Integer extends Comparable<Integer>, while
RClass<Object> b;
is not, since Object is not a class which extends comparable at all.
Yes, and bear in mind that objects of classes derived from Comparable ARE Comparable objects. Inheritance is a is-a relationship.
Simply put, the generic type T must be comparable in order to compareTo. otherwise you cannot do T.compareTo.
In Item 28 Effective java, it suggests: "always use Comparable<? super T> in preference to Comparable<T>. <T extends Comparable<? super T>>"
==>guhanvj, <T extends Comparable<T>> means that <T> is having upper bound of Comparable<T> objects. So <T> can have types of Byte, Character, Double, Float, Long, Short, String, and Integer classes which all implements Comparable<T> interface for natural ordering.

Java generics - what is the difference?

Consider that I have following interface:
public interface MyInterface<T extends Number>
In another class I want to declare a method like this:
public <T extends MyInterface<?>> void abc(T a);
Is this a correct way? Or maybe should I write:
public <T extends MyInterface<T>> void abc(T a);
What is the difference between these two declarations?
Some interfaces are intended to take the implementing class as the generic type.
For instance, the Comparable interface is usually implemented as:
class MyClass implements Comparable<MyClass>
because then the method
int compareTo(T o);
declared in Comparable can be used to compare an instance of MyClass to another instance of the same class.
Other interfaces have a generic parameter of a different type. For instance, List<T>, where T indicates the type of object contained within the list.
If you declare a method with:
public <T extends MyInterface<T>> void abc(T a);
then you're saying that the class T implements the interface MyInterface in the way that classes implement the Comparable interface - with themselves as the generic type. That might or might not be appropriate depending on what MyInterface actually is.
If you declare a method with:
public <T extends MyInterface<?>> void abc(T a):
then you not placing any constraint on the generic type in T's implementation of MyInterface.
The other option is
public void abc(MyInterface<?> a);
which is the simplest way to write a method that will accept any implementation of MyInterface.

Abstract Inheriting form of Comparable Interface

I understand how to use the Comparable<T> interface, but in this specific example, I'm wondering if there is a way to require the inheriting Class from an Abstract Class (which implements the Comparable<T> interface) to be Comparable against itself.
Let me reiterate, there is an Abstract Class that implements the Comparable Interface:
public abstract class MyAbstractClass implements Comparable<MyAbstractClass>
And a Class which inherits from this Abstract Class:
public class MyClass extends MyAbstractClass
Typically with this setup, the following method is required to satisfy the Comparable Interface:
public int compareTo(MyAbstractClass otherAbstractObject)
This requires me to make the following cast:
public int compareTo(MyAbstractClass otherAbstractObject)
{
MyClass otherObject = (MyClass) otherAbstractObject;
// Comparison...
return result;
}
Given the fact that this cast could easily fail by trying to use a different child of MyAbstractClass, I would like to be able to define my Abstract Class to accept the following method:
public int compareTo(MyClass otherMyObject)
{
// Comparison...
return result;
}
And ideas on how to accomplish this? Or is it simply not possible?
You can define MyAbstractClass using generics as follows:
public abstract class MyAbstractClass<T extends MyAbstractClass<T>>
implements Comparable<T> {
Then you can define subclasses such as
public class MyClass extends MyAbstractClass<MyClass>
which allow you to define the compareTo method like this:
public int compareTo(MyClass otherMyClass)
However, that doesn't prevent anyone from writing a subclass that doesn't conform to the pattern:
public class SneakyClass extends MyAbstractClass<MyClass>
which would also define the compareTo method similarly:
public int compareTo(MyClass otherMyClass)
Note: There's nothing that can force the generic type parameter of a class to equal the class on which it's defined; the best you can do is to enforce an upper bound on the abstract class, to at least force it to be some kind of MyAbstractClass.

Java generics syntax issue

I want to write a method that gets 2 parameters:
an interface.
a class that implements that interface.
I want it to be generic (compile time type safe).
is there a way?
if not, whats the alternative?
is there an option to get generic param that is an interface? ho do I declare it?
?
some thing like this ??
public <I, K extends I> void method(I i, K k){
}
In the above method first parameter would be an interface, and second parameter would be any class that implements that interface.
Interface1 i1;
method(i1, class1); //class1 implements Interface1
In generics, interface implementation and class extending is represented using extends keyword. there is no implements keyword in the world of generics.
public <T extends Interface> T myMethod(Interface I, Class<T> myClass) () {...}
if your method returns T

Java- The meaning of <T extends Comparable<T>>?

The full context being:
public class RClass<T extends Comparable<T>>
Would I be right in saying that the statement in the title means that the arguments plugged into the method must either be an object of a class which implements Comparable OR one of its derived classes?
Thanks.
This means that the type parameter must support comparison with other instances of its own type, via the Comparable interface.
An example of such a class is provided in the Oracle tutorial Object Ordering. Note the similar pattern to T extends Comparable<T> in the excerpt below:
public class Name implements Comparable<Name> {
...
public int compareTo(Name n) { ... }
}
Java- The meaning of <T extends Comparable<T>>?
a) Comparable <T>
is a generic interface (remember it's an "interface" i.e not a "class")
b) extends means inheritance from a class or an interface.
From above-said point#a, it is an interface..(Remember it is an inheritance from an "interface" i.e not from a "class")
c)From above-said both points #a & #b,
here "one interface" extends "another interface".
There should be an interface defined for this class..
just an example here is
interface MinMax<T extends Comparable<T>> {
T min();
T max();
}
d) now your class i.e public class RClass {} SHOULD
1# EITHER "implement" this "generic interface" Comparable<T> ..!!!
ex: public class RClass<T> implements Comparable<T>
2# OR create an interface and extend to this "generic interface" Comparable<T>
ex:
interface MinMax<T extends Comparable<T>> {
T min();
T max();
}
class RClass<T extends Comparable<T>> implements MinMax<T> {
.....
.....
}
Here,
Pay special attention to the way that the type parameter T is declared by RClass and
then passed to MinMax. Because MinMax requires a type that implements Comparable,
the implementing class (RClass in this case) must specify the same bound. Furthermore,
once this bound has been established, there is no need to specify it again in the implements clause.
Somewhere in that class, the programmer needs to write something like
if(t.compareTo(othert) < 0) {
...
}
For that to work, the type T must have a compareTo-method which compares it to another object of type T. Extending Comparable guarantees the existence of such a method, among other things.
It means that you can only create an instance of RClass with a type which quite literally extends Comparable<T>. Thus,
RClass<Integer> a;
is acceptable, since Integer extends Comparable<Integer>, while
RClass<Object> b;
is not, since Object is not a class which extends comparable at all.
Yes, and bear in mind that objects of classes derived from Comparable ARE Comparable objects. Inheritance is a is-a relationship.
Simply put, the generic type T must be comparable in order to compareTo. otherwise you cannot do T.compareTo.
In Item 28 Effective java, it suggests: "always use Comparable<? super T> in preference to Comparable<T>. <T extends Comparable<? super T>>"
==>guhanvj, <T extends Comparable<T>> means that <T> is having upper bound of Comparable<T> objects. So <T> can have types of Byte, Character, Double, Float, Long, Short, String, and Integer classes which all implements Comparable<T> interface for natural ordering.

Categories