• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Casting problem with LinkedHashMap !!

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic