Results 1 to 5 of 5
- 12-07-2012, 06:00 PM #1
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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?
Java 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) } }I like likes!.gif)
- 12-07-2012, 07:51 PM #2
Senior Member
- Join Date
- Nov 2012
- Posts
- 228
- Rep Power
- 1
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>
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:
andJava Code:private TimeSlotCollection(TimeSlotCollection<T> oClone) throws CloneNotSupportedException { for (T oSlot : oClone) { add((T) oSlot.clone()); } }
Java Code:@Override public boolean add(T oNewSlot) { try { return super.add((T) oNewSlot.clone()); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return false; }
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.
- 12-08-2012, 09:02 AM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Template error with <T extends xxx>
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
- 12-10-2012, 12:57 AM #5
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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! :)I like likes!.gif)
Similar Threads
-
Template in JSP
By jyothi.priyanka in forum New To JavaReplies: 0Last Post: 01-18-2011, 05:29 PM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By Java Tip in forum Java TipReplies: 0Last Post: 04-01-2008, 10:17 AM -
SWT code template
By Java Tip in forum Java TipReplies: 0Last Post: 12-30-2007, 12:18 PM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By JavaBean in forum Java TipReplies: 0Last Post: 09-28-2007, 12:56 PM -
Help with TLD template
By Albert in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-13-2007, 03:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks