java


Unable to Cast Map into Java Class


public class Model extends LinkedHashMap<String, Object>{
}
LinkedHashMap<String, Object> map = //instance of LinkedHashMap
Model model = (Model) map // Exception.
when I'm trying to cast LinkedHashMap<String,Object> into my Model class I'm getting class cast exception.
I'm basically using user defined method which expect Model as an argument that's why I'm casting my map into Model class
You cannot use LinkedHashMap where Model is expected, therefore you cannot cast.
Presumably Model has additional methods and more functionality than LinkedHashMap. Even if you did not add anything new in Model class yet, Java assumes that you will.
Possible solution: change
LinkedHashMap<String, Object> map = //instance of LinkedHashMap
to
LinkedHashMap<String, Object> map = //instance of Model
Another solution: change the code that uses Model class to use Map instead.
Still another approach: do not derive Model from Map, have Model own a Map. Change the code accordingly.
In general, you need to learn more about OO programming, especially the modern sort that deprecates inheritance in favor of aggregation.
You cannot do that because Model is not an instanceof LinkedHashMap. Think in terms of memory allocation. What have you actually done? You have allocated enough memory for a LinkedHashMap but NOT for a Model. The compiler is trying to save you from yourself.
API Specifications for the ClassCastException says:
Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.
class P {
}
class C extends P {
}
P p = new P();
C c = (C) p;//java.lang.ClassCastException; Runtime Exception
If you really want to do it. You do it in this way:
LinkedHashMap map = new Model();
map.put("one", "1");
Model model = (Model) map; // Exception.
System.out.println(model);
referece: Explanation of "ClassCastException" in Java

Related Links

Why does Java robot pressing Enter not work?
Display image on button click from InternalFrame
Enable/Disable Log4jLogger programmatically?
How to resolve unable to find valid certification path to requested target exception?
how to display records from database using jsp table
How to keep the program running if the user entered Y?
Dataset.map() throws ClassCastException while running in yarn-client mode from eclipse, but pass in local mode
Android pressed button, accelerate count
Convert Java Comparator to Scala Ordering
Java 8 flatmap unwrap multiple objects from collection
Sonar failed with an API incompatibility for Absent Code attribute
Trying to connect to a SQLite database, keep getting no Suitable Driver Found
Creating multiple Scenes with CLASS and calling them(classes)
SurvivorRatio flag of JVM does not work?
ORMLite IllegalArgumentException class is not valid for type
Java initialize variables “remotely”

Categories

HOME
ms-access
sendgrid
keras
server
heroku
comparison
iterator
framework7
jsp-tags
spring-cloud-stream
fsm
directx
handsontable
modx-revolution
hapi
quartz-scheduler
seaborn
saxon
object-detection
devops
emgucv
microsoft-sync-framework
react-css-modules
entitlements
social-media
libssl
frame
google-cloud-endpoints-v2
twilio-api
calibre
catch-all
caret
phpfreechat
mozilla
language-concepts
overriding
bitbucket-pipelines
space-complexity
mesos-chronos
pdf-reactor
no-www
angular2-meteor
babel-core
komodoedit
csound
knockout-components
windows-mobile-6.5
epson
jspdf-autotable
gnome-shell-extensions
migradoc
static-ip-address
heidisql
elements
url-masking
fody
measures
merge-conflict-resolution
impresspages
gridview-sorting
qcustomplot
yaws
apachebench
dataview
tform
separator
execute
wireshark-dissector
android-listview
ideamart
vstest.console.exe
fluid-dynamics
remobjects
key-management
fscommand
website-monitoring
knuth
google-style-guide
xcode-6.2
gulp-less
aapt
uv-mapping
gridfs
listings
opcache
ril
shellexecute
gridcontrol
orchardcms-1.7
labwindows
code-cleanup
bigcouch
mercurial-server
gnustep
hirefire
mongomapper
datareader
w3wp.exe
cxxtest
gamequery
xsdobjectgen
routedevent
multi-tier
kdbg
zend-decorators
commodore

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App