aspose file tools*
The moose likes Beginning Java and the fly likes Problem with output( Casting) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with output( Casting)" Watch "Problem with output( Casting)" New topic
Author

Problem with output( Casting)

Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
This is a question from enthuware's.



And the following declarations:
A a = new A();
B b = new B();
Identify options that will compile and run without error.


1. a = (B)(I)b;
2. b = (B)(I) a;
3. a = (I) b;
4. I i = (C) a;

I am not able to understand it's output.The correct answer is option 1.How? I assumed it is correct because b is an instance of B.No matter what's on the left side of = and not taking into consideration about(I) in the expression (B)(I)b

Option 2 is not correct beacuse a is not an instance of B.

Why is option 3 not correct.b is an instance of I(Indirectly).Look's like now my assumption of not looking on the left side of = is false and now it does matter to whom you are assigning the variable.
Option 4 is wrong because a is a A but A is not C.So we cannot cast this way.


Am i correct?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Option 3 is wrong because you cannot assign any arbitrary I to a variable of type A.

Class A implements interface I, but there could be other classes, unrelated to A, that also implement I. If option 3 were possible, you could do:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

Hi Rohan, why you are assuming that left side of = doesn't matter? Although your other assumptions seems to be correct only look at option C, you need to make your understanding about how a subclass casted to interface cant be assigned to superclass. Again superclass is not subclass.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2316
    
  49
1. a = (B)(I)b;

Type B is upcast to I - Legal as B extends A which implements I
Type I is downcast to B - Legal as the object in this case is of type B
Type B is assigned to variable of type A - Legal, you can always assigned an object to a variable which is a super type. ie it is always legal to upcast.

2. b = (B)(I) a;

Type A is upcast to I - Legal as A implements I
Type I is downcast to B - Illegal as the object in this case is of type A and can't be downcast to type B

3. a = (I) b;

Type B is upcast to I - Legal as B extends A which implements I
Type I is assigned to variable of type A - Illegal, you can't downcast unless you specify the type you are downcasting to.

4. I i = (C) a;

Type A is downcast to C - Illegal as the object in this case is of type a and can not be downcast to type C.

.Look's like now my assumption of not looking on the left side of = is false

Yes you are right in realising your assumption is wrong. See the answer in your other thread.
Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
Ok , i finally got it.
Thanks to everyone , especially Tony Docherty for nice and step by step explanation.
Chris Heinz
Greenhorn

Joined: Jul 07, 2014
Posts: 2
Hi folks,
Sorry for bumping such an old thread, but I got this question wrong(and the topic of casting is still giving me some troubles) on an enthuware mock test and just had a couple follow-up questions. Any help is appreciated.

Tony Docherty wrote:
1. a = (B)(I)b;

1. Type B is upcast to I - Legal as B extends A which implements I
2. Type I is downcast to B - Legal as the object in this case is of type B
Type B is assigned to variable of type A - Legal, you can always assigned an object to a variable which is a super type. ie it is always legal to upcast.

^^^1. So, here when you say, 'Type B' you are referring to the fact that 'b' is of type B?
^^^2. Again, when you say 'the object in this case is of type B', you mean that the object, 'b', is of Type B?


Tony Docherty wrote:
2. b = (B)(I) a;

Type A is upcast to I - Legal as A implements I
3. Type I is downcast to B - Illegal as the object in this case is of type A and can't be downcast to type B

^^^3. This is a bit confusing. Illegal as the object is of type A. I understand that 'a' can't be cast to B because A is a base class of B, but, the object, 'a' of type A was casted to 'I' just before this, so I would think that if 'I' can be cast to 'B', then all is well. Obviously, I'm wrong, but I can't get this to 'click'.

When comparing (B)(I)b with (B)(I)a, my take away is that regardless of the interface casting, 'b' can be cast to B(duh), and 'a' can not be cast to B. So, even after casting a or b to I, you still must look at the relationship between b and B or a and B to determine if casting is valid?


Tony Docherty wrote:
3. a = (I) b;

Type B is upcast to I - Legal as B extends A which implements I
4. Type I is assigned to variable of type A - Illegal, you can't downcast unless you specify the type you are downcasting to.

^^^4. It is obvious to me why this is not a valid statement, but Tony's explanation is a bit confusing. The way I think of it is that using a class A reference variable to refer to an Interface object is invalid. To me, that is why this is not valid. The verbage Tony uses, "can't downcast unless you specify the type you are downcasting to" does not click with me. Is it possible somebody could elaborate on this point?

Thanks for any help. Sorry for some of the newb questions. I just started studying recently. Trying to move out of embedded software and into the sw application world. Thanks again!
Chris
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2316
    
  49
Chris Heinz wrote:Hi folks,
Sorry for bumping such an old thread, but I got this question wrong(and the topic of casting is still giving me some troubles) on an enthuware mock test and just had a couple follow-up questions. Any help is appreciated.

Welcome to the Ranch.
Bumping the thread in this case is fine as your questions are about the content in the thread.

Chris Heinz wrote:
^^^1. So, here when you say, 'Type B' you are referring to the fact that 'b' is of type B?
^^^2. Again, when you say 'the object in this case is of type B', you mean that the object, 'b', is of Type B?

Yes. The object referenced by variable 'b' is of type 'B'

Chris Heinz wrote:
Tony Docherty wrote:
2. b = (B)(I) a;

Type A is upcast to I - Legal as A implements I
3. Type I is downcast to B - Illegal as the object in this case is of type A and can't be downcast to type B

^^^3. This is a bit confusing. Illegal as the object is of type A. I understand that 'a' can't be cast to B because A is a base class of B, but, the object, 'a' of type A was casted to 'I' just before this, so I would think that if 'I' can be cast to 'B', then all is well. Obviously, I'm wrong, but I can't get this to 'click'.

It doesn't matter what you legally cast an object to it is still the same object with the same type it's just how you are referring to it that changes. A reference of type 'I' can only be cast to type 'B' if the object is of type 'B' or a sub class of type 'B'

Chris Heinz wrote:
When comparing (B)(I)b with (B)(I)a, my take away is that regardless of the interface casting, 'b' can be cast to B(duh), and 'a' can not be cast to B. So, even after casting a or b to I, you still must look at the relationship between b and B or a and B to determine if casting is valid?

Yes

Chris Heinz wrote:
Tony Docherty wrote:
3. a = (I) b;

Type B is upcast to I - Legal as B extends A which implements I
4. Type I is assigned to variable of type A - Illegal, you can't downcast unless you specify the type you are downcasting to.

^^^4. It is obvious to me why this is not a valid statement, but Tony's explanation is a bit confusing. The way I think of it is that using a class A reference variable to refer to an Interface object is invalid. To me, that is why this is not valid. The verbage Tony uses, "can't downcast unless you specify the type you are downcasting to" does not click with me. Is it possible somebody could elaborate on this point?

The variable 'b' can be assigned to 'a' but once you have upcast it to type I you are trying to assign a reference of type I to a variable of type A which as you say is illegal. Therefore you need to explicitly downcast it to a type that can be assigned to 'a'. Does that make more sense?
Chris Heinz
Greenhorn

Joined: Jul 07, 2014
Posts: 2
Tony Docherty wrote:
It doesn't matter what you legally cast an object to it is still the same object with the same type it's just how you are referring to it that changes. A reference of type 'I' can only be cast to type 'B' if the object is of type 'B' or a sub class of type 'B'

Ah, of course. That makes sense.

Tony Docherty wrote:
The variable 'b' can be assigned to 'a' but once you have upcast it to type I you are trying to assign a reference of type I to a variable of type A which as you say is illegal. Therefore you need to explicitly downcast it to a type that can be assigned to 'a'. Does that make more sense?

That cleared things up. Thanks for your help, Tony.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with output( Casting)