permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting problem with LinkedHashMap !! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting problem with LinkedHashMap !!" Watch "Casting problem with LinkedHashMap !!" New topic
Author

Casting problem with LinkedHashMap !!

Prabhat Gupta
Ranch Hand

Joined: Jan 22, 2008
Posts: 135
Hi All,
When i m trying to run this code i am getting exception saying
java.lang.ClassCastException: java.util.HashMap




As I know LinkedHashMap is direct subclass of HashMap so casting can be done. Please suggest where i am doing mistake.
Hrishikesh Maluskar
Ranch Hand

Joined: Jun 19, 2008
Posts: 115
casting can be done but if you do downcating , it will always give classCastException.

try doing up casting as follows

package abc ;
import java.util.*;

public class H
{
public static void main(String[] args)
{
LinkedHashMap<Integer,String>lhm=new LinkedHashMap<Integer,String>();
lhm.put(new Integer(1),"abc");
System.out.println(lhm);

HashMap<Integer,String> hm= (HashMap<Integer,String> lhm;
System.out.println (lhm);
}
}


SCJP 1.5
www.licexpadvice.com
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234


check this code. This code is also throwing same exception.
If you know the reason why .... then same reason is there with your code
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234

casting can be done but if you do downcating , it will always give classCastException.


I doubt this statement.

check the example given below.

This is downcasting example and it is not throwing any exception.
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
case 1:

base b = new base();
sub s = (sub)b;

case 2:

base b = new sub();
sub s = (sub)b;


We can not perform downcasting in case one. Reason is very clear superclass object can not be cast to subclass.

In case 2, superclass reference variable refering to an object to subclass.
and subclass object can always cast to another subclass reference variable.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Prabhat Gupta:

As I know LinkedHashMap is direct subclass of HashMap so casting can be done.


it can not be... downcasting will give ClassCastException.

-----------------------------------------
LinkedHashMap is direct subclass of HashMap
-------------------------------------------
so, why you try to casting?... what is the reason ?


Hope This Helps
Prabhat Gupta
Ranch Hand

Joined: Jan 22, 2008
Posts: 135
Thanks all,
I got the point.if Object is of SuperClass type than it can't be downcasted to sub-class..as here i am doing try to cast HashMap type object to LinkedHashMap type .
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Indeed. Casting does not magically convert one kind of object (a HashMap in this case) to a different kind of object (a LinkedHashMap).

Casting is only a hint to the compiler where you express something like "Compiler, this object is really a LinkedHashMap, so I want you to treat it as that". At runtime, if the object really is not a LinkedHashMap, you'll get a ClassCastException.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting problem with LinkedHashMap !!
 
Similar Threads
LinkedHashMap program
Hash Map question ...
ClassCast exception
Problem with Jackson Parser
LinkedHashMap - trying to use the method removeEldestEntry