1

I have a broad question regarding Java casting via classes. Let's say I create 4 classes (well 3 classes and 1 interface), Interface A is the super interface I guess you could say and Class B implements A (meaning that it is the subclass of the interface A) and C extends B and then D extends C.

Let's say that I have a driver class in which I initialize the following like below:

A myA;
B myB = new B();
C myC = new C();
D myD = new D();
//I want to cast now!
myB = (B) myD;
myC = (D) myA;
myD = (C) myB;

When are these fabricated objects actually compilable? I'm having a bit of a difficult time understanding the rules between casting. I do kind of understand Down-casting and how it's not permitted, but I guess class casting is still a concept that sort of confuses me.

Stephen C
723k95 gold badges849 silver badges1.3k bronze badges
asked May 22, 2016 at 2:52
0

2 Answers 2

2

The thumb rule is that if an object B is of type A, then it can be casted to A. In you example B implements A so B is of type A. You can cast any B object to A. Since C extends B, C is of type B as well as of type A. So C objects can be cast to A or B.

answered May 22, 2016 at 3:14

Comments

0

Wanted to add as comment, but coz of limitation, had to add it as answer:

1) Rule is Child can inherit what father/parent has, but not reverse. 2) Child can be stored as parent, but not reverse.

That makes myD =(C)myAB; uncompilable as myD extends C (which extends B-->A)

So when you create

 B myB = new B();

If I try to explain in non technical terms, Then myB knows everything about B and A, but it does not know what is below. It can see and identify itself with anything above it in following hierarchy:

 A
 B
 C
 D

So lowest one D can be casted to anything that lies above it.

1) myB = (B) myD;

With rule state above, D is below B and hence can be assigned to B.

2) myC = (D) myA; Here you have casted interface to D, and hence in compile time, D can be assigned to top level C.

3) Here myB (which in that statement is typecasted to C in compile time) can't be assigned to D at compile time (although it is instance of D which is lower in hierarchy) but when you are compiling, you don't have runtime instance available. So below will fail:

myD = (C) myB;

I assume I have not confused you further here.

answered May 22, 2016 at 3:13

4 Comments

I kind of understand what you are saying although I'm still a little confused. Practically, what you are saying is that classes lower in the hierarchy know everything about their parent (or super classes) but parent classes can't be casted down because they don't know anything about their child classes. In the case of myD = (C) myB; isn't it being casted up to C still. I'm just a bit confused by all of this, that's all. Thanks!
Woho, And some smart guy negative voted the answer without any explanation :)
Yah I don't know why, I thought it was a good answer overall.
It is poorly and confusingly expressed, and the reason given why MyD = (C)myAB; doesn't compile is not correct, not to mention the fact that there is no myAB in the question.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.