GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java

 Print  |  
Question:  What's the difference between Object and "E"?

Answer: I'm trying to write an Iterator class for a LinkedList class that I wrote.
One of the methods is "public E next()"
I try to return an "E" but eclipse says "cannot convert from Object to E".
How do I fix this?


February 02, 2009 16:19:59 #4
 kabir soni   Member Since: February 2009    Total Comments: 3 

RE: What's the difference between Object and "E"?
 
Object is a class and E is a generic type parameter . And Iterator class alwayes return Object so to fix this problem, you need to call it. For example

import java.util.ArrayList;
import java.util.Iterator;


class list {
   ArrayList list = new ArrayList();
  
   public E next() {
     Iterator i = list.iterator();
     return (E)i.next();
   }
}
    
public class TestProgram {

   public static void main(String[] args) {
     list list = new list();
     list.list.add("A");
     System.out.println(list.next());
   }
    
}


     

 

Back To Question