Template error with <T extends xxx>
Eclipse shows an error in the method below. I do not really understand it, may someone explain it to me?
The clone() method returns an object of type TimeSlot.
As T extends TimeSlot it should go in fine in my understanding... :(
Any alternatives are welcome of course. Maybe I am just not seeing it?
Code:
public class TimeSlotCollection<T extends TimeSlot> extends ArrayList<T> implements Cloneable
{
// Clone another object
public TimeSlotCollection()
{
}
private TimeSlotCollection(TimeSlotCollection<T> oClone)
{
for (T oSlot : oClone) add([B]oSlot.clone()[/B]); // The method add(T) in the type TimeSlotCollection<T> is not applicable for the arguments (TimeSlot)
}
@Override
public boolean add(T oNewSlot)
{
return super.add(oNewSlot.clone()); // The method add(T) in the type TimeSlotCollection<T> is not applicable for the arguments (TimeSlot)
}
}
Re: Template error with <T extends xxx>
im not sure you can inherit from 2 classes in java, i maybe mistaken?
Re: Template error with <T extends xxx>
Quote:
Originally Posted by
Sierra
Eclipse shows an error in the method below. I do not really understand it, may someone explain it to me?
The clone() method returns an object of type TimeSlot.
As T extends TimeSlot it should go in fine in my understanding... :(
Any alternatives are welcome of course. Maybe I am just not seeing it?
Code:
public class TimeSlotCollection<T extends TimeSlot> extends ArrayList<T> implements Cloneable
{
// Clone another object
public TimeSlotCollection()
{
}
private TimeSlotCollection(TimeSlotCollection<T> oClone)
{
for (T oSlot : oClone) add(oSlot.clone());
}
@Override
public boolean add(T oNewSlot)
{
return super.add(oNewSlot.clone());
}
}
I'm no pro at this area of Java, and in fact far from it, but my experience with the clone method is that since it returns an Object type, you usually have to cast the reference returned. So shouldn't it be something like:
Code:
private TimeSlotCollection(TimeSlotCollection<T> oClone)
throws CloneNotSupportedException {
for (T oSlot : oClone) {
add((T) oSlot.clone());
}
}
and
Code:
@Override
public boolean add(T oNewSlot) {
try {
return super.add((T) oNewSlot.clone());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return false;
}
Quote:
Originally Posted by
monkeyjr97
im not sure you can inherit from 2 classes in java, i maybe mistaken?
I don't see anywhere where multiple inheritance is going on. Can you clarify this statement, as I don't see how it could apply to the current situation.
Re: Template error with <T extends xxx>
Quote:
Originally Posted by
Fubarable
I'm no pro at this area of Java, and in fact far from it, but my experience with the clone method is that since it returns an Object type[...]
You can override a method and define it to return a subclass of the original method (covariant return type), so it is right that the TimeSlot class or the subclasses can return an object of the type of the class (..."The clone() method returns an object of type TimeSlot.") :)
(but i think the cast to T is the right way here....)
I think monkeyjr97 saw two extends so he thought that he has two inheritances :)-:
Re: Template error with <T extends xxx>
It is difficult and the first time I use this kind of <T extends...>.
Right, the first "extends" is for the type T - the only errors/even warnings are the ones mentioned in the line comments. :)
eRaaaa is right that the basic method clone() is overwritten with a "TimeSlot clone()" within the TimeSlot class which worked so far. After thinking long about that problem I think that the <T extends TimeSlot> is somehow not applied to the method 'add' or not properly recognized... the question is, is that a bug or am I missing a logical conclusion?
It is also true that the thing gives only an unchecked warning if I do a cast to T - but usually it should not require this as T is declared as extending TimeSlot, right?
Thank you for your assistance anyway! :)