Class Lion extends Animal.
Here is my code:
Animal a = new Animal();
Lion b = new Lion();
Animal c = (Animal) b;
Animal[] arr = { a, b, c };
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i].getClass().getName());
arr[i].run();
}
The result is:
test2.Animal
Animal Run...
test2.Lion
Lion Run...
test2.Lion
Lion Run...
From the example seems that "c" is a "Lion", not an "Animal". Why is that happening?
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.
Casting from a subclass to a superclass is called upcasting. Upcasting is closely related to inheritance — another core concept in Java. It's common to use reference variables to refer to a more specific type. And every time we do this, implicit upcasting takes place.
Every child class has a parent class. Every instance of a child class has an instance of a parent class. A parent can be a child in another relationship. A parent class instance can exist without a child class instance.
Type casting is a way of converting data from one data type to another data type. This process of data conversion is also known as type conversion or type coercion. In Java, we can cast both reference and primitive data types. By using casting, data can not be changed but only the data type is changed.
From the example seems that "c" is a "Lion", not an "Animal". Why is that happening?
Because c
is a Lion:
Lion b = new Lion(); // Creates a Lion
Animal c = (Animal) b; // Refers to the Lion through an Animal variable
Now, c
is an Animal
-typed reference to a Lion
object. The object is still a Lion
, it's just the reference to it is limited to Animal
stuff. So when you ask that object what its class is (not what your interface to it is in the c
variable / third entry in your array), it tells you it's a Lion
.
This is exactly like this situation:
Map m = new HashMap();
The m
reference is typed Map
and so you can only use it to access the things the Map
interface defines, but the object it's referring to is a HashMap
instance.
A cast doesn't make the referenced object change its type, it just restricts itself to the methods of the supertype. You couldn't cast a Banana
to an Animal
for these reasons.
The cast in your line
Animal c = (Animal) b;
happens automatically anyways. You just need to specify your cast when you downcast:
Animal a = new Dog();
Dog d = (Dog) a;
But both a
and d
still point to a Dog
in the heap and will thus use the instance methods of the Dog
class if they override the methods of the Animal
class.
In other words, a
is a Dog
, but as long as it is declared as (or typecast to) an Animal
, it can only use Animal
methods.
You are invoking getClass
.
Method invocation resolves at runtime, hence it prints Lion
and not 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