classB cannot be cast to classC : java.lang.ClassCastException











up vote
-5
down vote

favorite












I have three classes:



When I run this code, I got this error:




java.lang.ClassCastException: classB cannot be cast to classC




What is the reason of this error and how to solve it?



public classB extends classA
}


}

public classC extends classB
}


}

public classX extends classD
}


}


...



Not working



public static void main(String args) throws Exception {

aa = new classX<Double>();
((ClassC<Double>) aa).set();
MM MMobj = ((ClassC<Double>) aa).get();









share|improve this question









New contributor




user10608907 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 4




    You simply can't cast an object to class it isn't an instance of. A direct instance of RN is not an instance of RNM, so you can't cast it to one.
    – khelwood
    Nov 5 at 17:05










  • @khelwood would you please give an example how it should be?
    – user10608907
    Nov 5 at 17:07






  • 2




    An example of what? I've got no idea what you are trying to do
    – khelwood
    Nov 5 at 17:08










  • @khelwood create subclass RNM which extends RN class, then run this ((RNM<DoubleSol>) algo).setRF(new ArrayFr(rPF)); in the main() method
    – user10608907
    Nov 5 at 17:13










  • All you've shown from your main is the cast, and no indication what rPF is, where algo comes from, or why you're trying to cast it. Post a Minimal, Complete, and Verifiable example that shows what you're trying to do.
    – khelwood
    Nov 5 at 17:48















up vote
-5
down vote

favorite












I have three classes:



When I run this code, I got this error:




java.lang.ClassCastException: classB cannot be cast to classC




What is the reason of this error and how to solve it?



public classB extends classA
}


}

public classC extends classB
}


}

public classX extends classD
}


}


...



Not working



public static void main(String args) throws Exception {

aa = new classX<Double>();
((ClassC<Double>) aa).set();
MM MMobj = ((ClassC<Double>) aa).get();









share|improve this question









New contributor




user10608907 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 4




    You simply can't cast an object to class it isn't an instance of. A direct instance of RN is not an instance of RNM, so you can't cast it to one.
    – khelwood
    Nov 5 at 17:05










  • @khelwood would you please give an example how it should be?
    – user10608907
    Nov 5 at 17:07






  • 2




    An example of what? I've got no idea what you are trying to do
    – khelwood
    Nov 5 at 17:08










  • @khelwood create subclass RNM which extends RN class, then run this ((RNM<DoubleSol>) algo).setRF(new ArrayFr(rPF)); in the main() method
    – user10608907
    Nov 5 at 17:13










  • All you've shown from your main is the cast, and no indication what rPF is, where algo comes from, or why you're trying to cast it. Post a Minimal, Complete, and Verifiable example that shows what you're trying to do.
    – khelwood
    Nov 5 at 17:48













up vote
-5
down vote

favorite









up vote
-5
down vote

favorite











I have three classes:



When I run this code, I got this error:




java.lang.ClassCastException: classB cannot be cast to classC




What is the reason of this error and how to solve it?



public classB extends classA
}


}

public classC extends classB
}


}

public classX extends classD
}


}


...



Not working



public static void main(String args) throws Exception {

aa = new classX<Double>();
((ClassC<Double>) aa).set();
MM MMobj = ((ClassC<Double>) aa).get();









share|improve this question









New contributor




user10608907 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have three classes:



When I run this code, I got this error:




java.lang.ClassCastException: classB cannot be cast to classC




What is the reason of this error and how to solve it?



public classB extends classA
}


}

public classC extends classB
}


}

public classX extends classD
}


}


...



Not working



public static void main(String args) throws Exception {

aa = new classX<Double>();
((ClassC<Double>) aa).set();
MM MMobj = ((ClassC<Double>) aa).get();






java oop






share|improve this question









New contributor




user10608907 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




user10608907 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 7 at 13:05





















New contributor




user10608907 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 5 at 17:03









user10608907

42




42




New contributor




user10608907 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user10608907 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user10608907 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 4




    You simply can't cast an object to class it isn't an instance of. A direct instance of RN is not an instance of RNM, so you can't cast it to one.
    – khelwood
    Nov 5 at 17:05










  • @khelwood would you please give an example how it should be?
    – user10608907
    Nov 5 at 17:07






  • 2




    An example of what? I've got no idea what you are trying to do
    – khelwood
    Nov 5 at 17:08










  • @khelwood create subclass RNM which extends RN class, then run this ((RNM<DoubleSol>) algo).setRF(new ArrayFr(rPF)); in the main() method
    – user10608907
    Nov 5 at 17:13










  • All you've shown from your main is the cast, and no indication what rPF is, where algo comes from, or why you're trying to cast it. Post a Minimal, Complete, and Verifiable example that shows what you're trying to do.
    – khelwood
    Nov 5 at 17:48














  • 4




    You simply can't cast an object to class it isn't an instance of. A direct instance of RN is not an instance of RNM, so you can't cast it to one.
    – khelwood
    Nov 5 at 17:05










  • @khelwood would you please give an example how it should be?
    – user10608907
    Nov 5 at 17:07






  • 2




    An example of what? I've got no idea what you are trying to do
    – khelwood
    Nov 5 at 17:08










  • @khelwood create subclass RNM which extends RN class, then run this ((RNM<DoubleSol>) algo).setRF(new ArrayFr(rPF)); in the main() method
    – user10608907
    Nov 5 at 17:13










  • All you've shown from your main is the cast, and no indication what rPF is, where algo comes from, or why you're trying to cast it. Post a Minimal, Complete, and Verifiable example that shows what you're trying to do.
    – khelwood
    Nov 5 at 17:48








4




4




You simply can't cast an object to class it isn't an instance of. A direct instance of RN is not an instance of RNM, so you can't cast it to one.
– khelwood
Nov 5 at 17:05




You simply can't cast an object to class it isn't an instance of. A direct instance of RN is not an instance of RNM, so you can't cast it to one.
– khelwood
Nov 5 at 17:05












@khelwood would you please give an example how it should be?
– user10608907
Nov 5 at 17:07




@khelwood would you please give an example how it should be?
– user10608907
Nov 5 at 17:07




2




2




An example of what? I've got no idea what you are trying to do
– khelwood
Nov 5 at 17:08




An example of what? I've got no idea what you are trying to do
– khelwood
Nov 5 at 17:08












@khelwood create subclass RNM which extends RN class, then run this ((RNM<DoubleSol>) algo).setRF(new ArrayFr(rPF)); in the main() method
– user10608907
Nov 5 at 17:13




@khelwood create subclass RNM which extends RN class, then run this ((RNM<DoubleSol>) algo).setRF(new ArrayFr(rPF)); in the main() method
– user10608907
Nov 5 at 17:13












All you've shown from your main is the cast, and no indication what rPF is, where algo comes from, or why you're trying to cast it. Post a Minimal, Complete, and Verifiable example that shows what you're trying to do.
– khelwood
Nov 5 at 17:48




All you've shown from your main is the cast, and no indication what rPF is, where algo comes from, or why you're trying to cast it. Post a Minimal, Complete, and Verifiable example that shows what you're trying to do.
– khelwood
Nov 5 at 17:48

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






user10608907 is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53158948%2fclassb-cannot-be-cast-to-classc-java-lang-classcastexception%23new-answer', 'question_page');
}
);

Post as a guest





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes








user10608907 is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















user10608907 is a new contributor. Be nice, and check out our Code of Conduct.













user10608907 is a new contributor. Be nice, and check out our Code of Conduct.












user10608907 is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53158948%2fclassb-cannot-be-cast-to-classc-java-lang-classcastexception%23new-answer', 'question_page');
}
);

Post as a guest




















































































這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini