I am helping a friend from first year to prepare his exam on Java. The teacher asked them to create a method that can cast a Musicians to a Poets.
No one knew how to do it. After thinking a good while, I came to the conclusion that it is impossible to do that(ClassCastException), because the fact that Musician and Poet Share an Interface is not enough to cast them to the other.
I think that would be possible only, if they were in the same inheritance chain.
Now I have three questions:
ClassCastException
. You could try:
Musician musician = new Musician();
Artist artist = musician;
Poet poet = (Poet)artist;
But of course, that code won't actually work at runtime.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With