Results 1 to 2 of 2
  1. #1
    gvm
    gvm is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default Type wildcards in Generic

    Hi
    I would appreciate if any one would please correct whats wrong in my code. at line // <<<<< ERROR



    Java Code:
    	////////////////////////////////////////////
    	// Type wild cards
    	
    	class Base<T>
    	{
    		protected LinkedList<T> items = new LinkedList<T>();
    		public void enqueue(T item){
    			items.addLast(item);
    		}
    		public T dequeue(){
    			return items.removeFirst();
    		}
    		
    		public boolean isEmply(){
    			return (items.size() == 0);
    		}
    		public void addAll(Collection<? extends T> coll){
    			// add all items from coll to the end of the queue
    			for(T item : coll)
    				enqueue(item);
    		}
    	} // end of class Base
    	
    	class A<T> extends Base<T>{
    		public void enqueue(T item){
    			System.out.println("Class A");
    			items.addLast(item);
    		}
    		public T dequeue(){
    			System.out.println("Class A");
    			return items.removeFirst();
    		}
    		
    		public boolean isEmply(){
    			System.out.println("Class A");
    			return (items.size() == 0);
    		}
    		public void addAll(Collection<? extends T> coll){
    			System.out.println("Class A");
    			// add all items from coll to the end of the queue
    			for(T item : coll)
    				enqueue(item);
    		}
    	} //end of class A
    	
    	class B<T> extends Base<T>{
    		public void enqueue(T item){
    			System.out.println("Class B");
    			items.addLast(item);
    		}
    		public T dequeue(){
    			System.out.println("Class B");
    			return items.removeFirst();
    		}
    		public boolean isEmpty(){
    			System.out.println("Class B");
    			return (items.size() == 0);
    		}
    		public void addAll(Collection<? extends T> coll){
    			System.out.println("Class B");
    			// add all items of coll to the endof the queue
    			for(T item: coll)
    				enqueue(item);
    		}
    	} // endof class B
    	
    	Base<Integer> intBase = new Base<Integer>();
    	A<Integer> intA = new A<Integer>();
    	B<Integer> intB = new B<Integer>();
    	
    	intA.enqueue(10); //<<<<< ERROR

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    What was the compiler whining about?

    kind regards,

    Jos (<--- too lazy to copy/paste, correct and compile)

Similar Threads

  1. Generic return type is not inferred in for-each loop
    By ranma173 in forum Advanced Java
    Replies: 5
    Last Post: 10-08-2010, 11:50 AM
  2. Generic collection type
    By dreuzel in forum Advanced Java
    Replies: 7
    Last Post: 12-23-2009, 06:05 PM
  3. Generics and Wildcards
    By ajeeb in forum Advanced Java
    Replies: 2
    Last Post: 01-30-2009, 11:00 PM
  4. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 10:43 AM
  5. Replies: 1
    Last Post: 05-16-2008, 11:06 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
  •