I have a misunderstanding about typecasting in Java language. The problem is ClassCastException. For example, in this code, assuming Animal is the parent class of the Dog class,
Animal animal = new Animal();
Dog dog = (Dog) animal;
throws ClassCastException after execution. However, while studying android packages, I found an example about typecasting which should throw a ClassCastException, considering that java example.
EditText editText = (EditText) findViewById(R.id.edit_message);
In this code, findViewById method returns a View class object, which is one of the superclasses of EditText class.(from android.view.View to android.widget.EditText) The code runs fine. Could anyone explain if I made a mistake or how this happens?
Thanks in advance.
polymorphism
tag, you know how to search the concept on this site or elsewhere. Seriously, this is too basic. Search before you post. – Virelay