Results 1 to 5 of 5
  1. #1
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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!

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Template error with <T extends xxx>

    im not sure you can inherit from 2 classes in java, i maybe mistaken?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Template error with <T extends xxx>

    Quote Originally Posted by Sierra View Post
    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(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:

    Java Code:
       private TimeSlotCollection(TimeSlotCollection<T> oClone)
             throws CloneNotSupportedException {
          for (T oSlot : oClone) {
             add((T) oSlot.clone());
          }
       }
    and

    Java 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 View Post
    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.

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Template error with <T extends xxx>

    Quote Originally Posted by Fubarable View Post
    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

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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!

Similar Threads

  1. Template in JSP
    By jyothi.priyanka in forum New To Java
    Replies: 0
    Last Post: 01-18-2011, 05:29 PM
  2. Replies: 0
    Last Post: 04-01-2008, 10:17 AM
  3. SWT code template
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-30-2007, 12:18 PM
  4. Replies: 0
    Last Post: 09-28-2007, 12:56 PM
  5. Help with TLD template
    By Albert in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-13-2007, 03:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •