This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting problem with LinkedHashMap !!

 
Prabhat Gupta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic