Results 1 to 3 of 3
  1. #1
    castiel is offline Member
    Join Date
    Aug 2009
    Posts
    26
    Rep Power
    0

    Default Generics Problem (Bound Mismatch)

    Hey, could someone please tell me why I am getting a bound mismatch
    when I am passing an object which DOES extend Box2 and implement
    MyInterface. The following is my code. I'm just playing around with generics atm and trying to get around how to use it. I've enclosed my error that
    I'm getting from Eclipse in the coding comments below.


    Java Code:
    public class GenericExtender<T extends Box2 & MyInterface> {
    
    public static <U extends Box2 & MyInterface> void print(U u){
    	System.out.println(u.getClass().getName());
    }
    	public static void main(String[] args){
    
    		GenericExtenderRun ger = new GenericExtenderRun();
    		
    		/*
    		Bound mismatch: The generic method print(U) of type GenericExtender<T> is not applicable for the arguments 
    		(GenericExtenderRun). The inferred type GenericExtenderRun is not a valid substitute for the bounded parameter 
    		<U extends Box2 & MyInterface>
    		*/
    		GenericExtender.print(ger); 
    	}
    }
    
    
    public class GenericExtenderRun extends Number implements MyInterface{
    
    	@Override
    	public int intValue() {
    		// TODO Auto-generated method stub
    		return 0;
    	}
    
    	@Override
    	public long longValue() {
    		// TODO Auto-generated method stub
    		return 0;
    	}
    
    	@Override
    	public float floatValue() {
    		// TODO Auto-generated method stub
    		return 0;
    	}
    
    	@Override
    	public double doubleValue() {
    		// TODO Auto-generated method stub
    		return 0;
    	}
    	
    	@Override
    	public void printA() {
    		// TODO Auto-generated method stub
    	}
    }
    Thanks,
    newbie

  2. #2
    castiel is offline Member
    Join Date
    Aug 2009
    Posts
    26
    Rep Power
    0

    Default Don't worry. Problem SOLVED and I'm an Idiot ><

    Sorry, I've been changing the code tooo much and didn't realise
    I had extended Box2 in one and Number in another.

    thanks

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    As an aside, is the Number class one you have written yourself of the one in the Java API? If you have written it yourself then consider a different name as it always leads to confusion when you give a custom class the same name as a API class.

Similar Threads

  1. Object mismatch, not sure why
    By olddog in forum New To Java
    Replies: 1
    Last Post: 01-29-2011, 10:18 PM
  2. Generics problem
    By ankur.trapasiya in forum New To Java
    Replies: 2
    Last Post: 01-22-2011, 05:09 PM
  3. Type Mismatch error
    By and0rsk in forum New To Java
    Replies: 2
    Last Post: 10-10-2010, 11:16 AM
  4. printable area mismatch between Java & C++ possible?
    By r00tb33r in forum AWT / Swing
    Replies: 0
    Last Post: 06-16-2010, 06:29 AM
  5. Replies: 2
    Last Post: 11-16-2007, 01:24 PM

Posting Permissions

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