Java help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Java help

Classcastexception??? why do i get Classcastexception error for this?could someone explain please...

27th Oct 2017, 2:54 AM
1 Answer
+ 3
CHANGE: Pokemon p1=new Pokemon(); ((Pikachu)p1).sound(); TO: Pokemon p1 = new Pokemon(); p1.sound(); ^p1 is already of the Pokemon class type, so you don't need to cast it. That'll fix your problem.
27th Oct 2017, 3:28 AM