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.
Therefore, there is an “is-a” relationship between the child and parent. Therefore, the child can be implicitly upcasted to the parent. However, a parent may or may not inherits the child's properties. However, we can forcefully cast a parent to a child which is known as downcasting.
Parent to Child (Explicit casting - Can be successful) Note: Because objects has polymorphic nature, it is possible for a variable of a parent class type to hold a child type. Conclusion : After reading above all, hope it will make sense now like how parent to child conversion is possible(Case 3).
Type Casting is a feature in Java using which the form or type of a variable or object is cast into some other kind or Object, and the process of conversion from one type to another is called Type Casting.
If you override a parent method in its child, child objects will always use the overridden version. But; you can use the keyword super to call the parent method, inside the body of the child method. This would print: I'm the child.
Once you create an object, you can't change its type. That's why you can't cast an Animal to a Dog.
However, if you create an object of a sub-class, you can keep a reference to it in a variable of the super-class type, and later you can cast it to the sub-class type.
This will work :
Animal a = new Dog ();
Dog d = (Dog) a;
In the Android example, you have a layout resource that looks like this :
<EditText
android:id="@+id/edit_message"
..."/>
This definition will cause Android to create an instance of EditText
, and therefore you can cast the view returned by findViewById
to EditText
. You can't cast it to anything else that isn't a super-type of EditText
.
Basically you can't cast an instance of a superclass to a subclass because the instance of a subclass is not yet known. Upcasting is a sure way to prevent this exception to happen because we are now dealing polymorphism to our code.
You must instance a subclass first:
Dog dog = new Dog;
We can hide the methods of the class Dog not found to its parent class Animal by casting it to its superclass:
Animal animal = (Animal) dog;
Then you can downcast this back to your subclass Dog because the instance of its subclass is already known:
Dog anotherDog = (Dog) animal;
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