Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java cast child to parent

Tags:

java

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?

like image 384
alpha09 Avatar asked Feb 12 '15 14:02

alpha09


People also ask

Can child be cast to parent in Java?

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.

Can you cast a subclass to a superclass?

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.

Can a parent class be an instance of a child class?

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.

How does type casting work in Java?

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.


3 Answers

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.

like image 157
T.J. Crowder Avatar answered Oct 22 '22 09:10

T.J. Crowder


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.

like image 21
ling_jan Avatar answered Oct 22 '22 09:10

ling_jan


You are invoking getClass.

Method invocation resolves at runtime, hence it prints Lion and not Animal.

like image 34
Mena Avatar answered Oct 22 '22 10:10

Mena