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

How do I add an item to a linked list in Java?
Java mechanisms at use in lambdaj closures
Ways to fill a list in Java
Hibernate ConstraintViolationException in join table
Creating an X509 Certificate in Java without BouncyCastle?
java.lang.OutOfMemoryError: Java heap space in jasper report
eclipse 3.4 can't start
What is the best way to call repaint() at regular intervals in my Java2D program?
Sharing properties file from a single location
How can I load file into web app through certain periods?
How can I rename a file in Java?
how to set SqlMapClient outside of spring xmls
How to create a folder in a directory where user need admin permission
How do I add a menubar to a tabbed pane?
Calling a DLL from an Applet via JNI
whether reading swing is worth it?

Categories

HOME
winforms
date
atom-editor
zeromq
openstack
server
deezer
dot
tesseract
mean-stack
android-4.4-kitkat
at-command
fsm
webrequest
cloudkit
qt-creator
gz
size
flask-wtforms
percona
highlight.js
facebook-page
jprofiler
dbext
emulator
opencover
cloudhub
tar
mustache.php
icloud-api
firebase-crash-reporting
sqlcipher
sparse-matrix
rst2pdf
fopen
elasticsearch-ruby
replaceall
fog
android-ble
io-redirection
xenforo
dism
pdf-reactor
nand2tetris
nomethoderror
gpx
businessworks
wso2carbon
chain-builder
namecoin
qtwebview
flutterwave
imanage
mcafee
vtigercrm
gnome-shell-extensions
objective-c-swift-bridge
wdf
vmware-tools
underscore.js-templating
gstreamer-0.10
nsviewcontroller
nessus
oauth2client
rhino-servicebus
two-factor-authentication
browser-link
srand
maven-tomcat-plugin
execute
feedback
ibaction
sniffer
mmc
pretty-print
c++03
file-copying
cctv
asp.net-web-api-odata
device-manager
google-reader
reactfx
seaside
jboss-weld
ember-charts
monomac
aqtime
soundtouch
coverflow
dataservice
xamlparseexception
mt
automount
enter
code-cleanup
gnu-prolog
deobfuscation
heartbeat
yui-datatable
modelstate
firefox4
sef
kdbg
eqatec
ubuntu-9.04
dbisam
django-notification
wsdl.exe
misv

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App