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();
java oop
New contributor
|
show 5 more comments
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();
java oop
New contributor
4
You simply can't cast an object to class it isn't an instance of. A direct instance ofRN
is not an instance ofRNM
, 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 yourmain
is the cast, and no indication whatrPF
is, wherealgo
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
|
show 5 more comments
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();
java oop
New contributor
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
java oop
New contributor
New contributor
edited Nov 7 at 13:05
New contributor
asked Nov 5 at 17:03
user10608907
42
42
New contributor
New contributor
4
You simply can't cast an object to class it isn't an instance of. A direct instance ofRN
is not an instance ofRNM
, 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 yourmain
is the cast, and no indication whatrPF
is, wherealgo
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
|
show 5 more comments
4
You simply can't cast an object to class it isn't an instance of. A direct instance ofRN
is not an instance ofRNM
, 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 yourmain
is the cast, and no indication whatrPF
is, wherealgo
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
|
show 5 more comments
active
oldest
votes
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 ofRNM
, 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 whatrPF
is, wherealgo
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