Code Comments
Programming Forum and web based access to our favorite programming groups.Hi! By using the class LinkedList, I wrote a method, that returns a list of Elements of the type of a class, I wrote too. Now I want to access a method of the first Element by using the method getFirst() (this should return an element of the type of the class in my opinion) and then the method name. But as the type of the returned Element is now Object, I can only access the methods of the class Object and not of the class, I thought my Elements would be a type of. Thank you.
Post Follow-up to this messageHi Heiko, That's because LinkedList only knows how to link Objects. But since you happen to know that your Element is also an Element, you can cast it back to an Element: Element xxx = (Element)list.getFirst(); If you are using JDK 1.5 (J2SE 5.0), you could also have a look at generics, which offers a much nicer and safer solution. See http://java.sun.com/developer/techn.../J2SE/generics/ Good luck, Paul. On 27-10-2004 12:40, Heiko Fleischmann wrote: > Hi! > By using the class LinkedList, I wrote a method, that returns a list of > Elements of the type of a class, I wrote too. > Now I want to access a method of the first Element by using the method > getFirst() (this should return an element of the type of the class in my > opinion) and then the method name. But as the type of the returned > Element is now Object, I can only access the methods of the class Object > and not of the class, I thought my Elements would be a type of. > Thank you.
Post Follow-up to this messagePaul H. van Rossem wrote: > Hi Heiko, > > That's because LinkedList only knows how to link Objects. But since you > happen to know that your Element is also an Element, you can cast it > back to an Element: > Element xxx = (Element)list.getFirst(); > If you are using JDK 1.5 (J2SE 5.0), you could also have a look at > generics, which offers a much nicer and safer solution. > See http://java.sun.com/developer/techn.../J2SE/generics/ > Good luck, Paul. > Hi Paul! Thank you for your quick response! I think I got it now. Best regards! Heiko.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.