File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList class

 
memati bas
Ranch Hand
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I noticed that ArrayList class does not have toString() method. But, in spite of this, when we use get() method in loops it behaves like that it has a toString method().
Why???

 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All class definitions extends Object, and since Object has the public method toString, every class definition contains the public method toString
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the get method simply returns an element contained in the ArrayList, so the toString implementation depends on the element type. It has nothing to do with how toString is implemented in ArrayList.

But as a side note, the toString implementation in ArrayList is inherited from AbstractCollection.
 
memati bas
Ranch Hand
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks your replies but I have still some problems. Although, we do not override toString method , there is no any problem derived. Why??
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by memati bas:
... Although, we do not override toString method , there is no any problem derived. Why??

Derived classes can override toString, but they are not required to (because the method is not abstract). Without overriding, the inherited implementation is used.

Does that answer the question?
[ February 02, 2006: Message edited by: marc weber ]
 
memati bas
Ranch Hand
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh , thaanks.
I think I understood it.
 
memati bas
Ranch Hand
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Latly , even though we only use the members of an object while using get() method, I think we can also use instance variables which do not belong to the objevt class indirectly, is not it??
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by memati bas:
Latly , even though we only use the members of an object while using get() method, I think we can also use instance variables which do not belong to the objevt class indirectly, is not it??

An ArrayList can hold any type of object reference. In fact, when a reference is added to the List, its type is automatically upcast to Object, which is the base class of all objects.

Prior to Java 1.5, when getting an element from a List, you needed to explicitly downcast the reference back to the desired type.

Since Java 1.5, Lists ensure type safety with generics. So you can create an ArrayList that will only allow specific types to be added, and the downcast is handled automatically.

Here's an illustration...
 
memati bas
Ranch Hand
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh,
Thanks, thanks and thanks for your explainatory replies Marc.

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic