File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Typecast Object type to user defined type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Typecast Object type to user defined type" Watch "Typecast Object type to user defined type" New topic

Typecast Object type to user defined type

K Sujit
Ranch Hand

Joined: Mar 23, 2005
Posts: 33
Suppose you created a class and put objects of it in a ArrayList when u retrieve from the arraylist u will get Object type not your class types so how will we get our class type from Object types.
See Compile error incompatible types
found : java.lang.Object
required: PointList
PointList pTemp =;

//My Code

import java.util.*;

class PointList extends ArrayList{

class SegmentList extends ArrayList{

public class StudyArrayList{
public static void main(String args[]){

SegmentList s = new SegmentList();
PointList p = new PointList();

p.add(new Double(492661.56250));
p.add(new Double(496868.65625));
p = new PointList();
p.add(new Double(426870.80903));
p.add(new Double(-432341.89723));

Iterator sI = s.iterator();
while (sI.hasNext()) {
System.out.println("Next Segment");
PointList pTemp =;
Iterator pI = pTemp.iterator();

I wish java run in my veins.
marc weber

Joined: Aug 31, 2004
Posts: 11343

You need to explicitly cast the object back to its correct type...

PointList pTemp = (PointList);

Also, please use code tags to keep formatting intact...

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Make sure you are clear that the object you get and the reference to it are distinct items. The reference type determines what methods you can call. The toUpperCase fails here and the reasons should be clear.

String foo = "boo";
Object bar = foo;

As far as arrays, you should use Java 5.0 to minimize casting. This really has become "basic" Java now:

List<String> list = new ArrayList<String>();

Couple things. First use the interface type for references where possible. Next, tell the compiler what type of List you are referring to and create one that is compatible with the reference: hence, String appears twice.

String x = list.get(0); // Cast is no longer required
marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Rick O'Shay:
Make sure you are clear that the object you get and the reference to it are distinct items...

That's a good point. To be clear, I probably should have said, "You need to explicitly cast the reference of type Object back to its correct type..."
Philip Heller
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
I recommend using generic collections whenever possible. Since your list contains only instances of Double, you can do this:

The syntax takes some getting used to. The type is no longer "ArrayList" but "ArrayList<Double>", which I pronounce as "ArrayList of Double". The compiler forbids adding anything that isn't instanceof Double, thus catching many bugs. And when you retrieve from the collection, you don't need to cast.

In your case, you extended ArrayList. To use generics, your class declaration becomes:

More in Chapter 8 of "Complete Java 2 Certification".

Hope this helps.
-- Phil

Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
Could you also use the new JDK 1.5 "for each" construct to shorten the code to something like this?

Is this equivilent to the double iterator usage in the code above?
I agree. Here's the link:
subject: Typecast Object type to user defined type
It's not a secret anymore!