• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

reference casting

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any help with reference variable casting is much appreciated.

Here is the question from K&B that threw me off:

Given:
class Putter{
public static void main(String[]args){
Bango b1=new Bango();
Bango b2=new Bongo();
Bango b3=new Bingo();
//insert code
}
}
class Bango{}
class Bongo extends Bango{}
class Bingo extends Bongo{}
Which, inserted at "//insert code", will NOT compile?(Choose all that apply.)

A.Bongo b4=b2
B.Bongo b5=(Bongo)b2
C.Bango b6=b3
D.Bango b7=(Bango)b3
E.Bingo b8=b3
F.Bingo b9=(Bingo)b3
G.Bango b10=(Bongo)b3

Answer: A and E will not compile.

For one, I guess I don't understand how G can work.

Secondly, Is there a shortcut or some other way to think of it that may help me wrap my mind around the concept?

Help.
thanks,
brian
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Brian,

Any help with reference variable casting is much appreciated.

Here is the question from K&B that threw me off:

Given:
class Putter{
public static void main(String[]args){
Bango b1=new Bango();
Bango b2=new Bongo();
Bango b3=new Bingo();
//insert code
}
}
class Bango{}
class Bongo extends Bango{}
class Bingo extends Bongo{}
Which, inserted at "//insert code", will NOT compile?(Choose all that apply.)

A.Bongo b4=b2
B.Bongo b5=(Bongo)b2
C.Bango b6=b3
D.Bango b7=(Bango)b3
E.Bingo b8=b3
F.Bingo b9=(Bingo)b3
G.Bango b10=(Bongo)b3

Answer: A and E will not compile.

For one, I guess I don't understand how G can work.

Secondly, Is there a shortcut or some other way to think of it that may help me wrap my mind around the concept?



Remember this : During compile time compiler checks on reference variable type and during run-time its based on object type.

Bango b3=new Bingo();

G.Bango b10=(Bongo)b3

b3 is a reference type of Bango and the object is of Bingo.In the code Bongo class extends Bango. So during compile time the cast passes the IS-A test. So the cast will compile.


Regards
NIK
SCJP 1.5
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian



Which, inserted at "//insert code", will NOT compile?(Choose all that apply.)

A.Bongo b4=b2
B.Bongo b5=(Bongo)b2
C.Bango b6=b3
D.Bango b7=(Bango)b3
E.Bingo b8=b3
F.Bingo b9=(Bingo)b3
G.Bango b10=(Bongo)b3

Answer: A and E will not compile.

For one, I guess I don't understand how G can work.

Secondly, Is there a shortcut or some other way to think of it that may help me wrap my mind around the concept?



Technically, all of the answers are correct since the question asks which WILL NOT COMPILE, and the semi-colon is missing for all of them

G doesn't work, but it does COMPILE (as Nik has said).

Simply put, anything with an explicit cast will COMPILE (but may throw an exception at runtime) - provided the cast type and the reference type are in the same hierarchy. So B, D, F and G all compile.

If the reference type of the object being assigned does not match or is not a subclass of the reference type being assigned to, it will not compile without a cast. So A (assigning a Bango to a Bongo) and E (assigning a Bango to a Bingo) do not compile, as they are assigning a superclass to a subclass.
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Anthony

Why wont G work?

its perfectly legal at runtime as well.

The object in b3 is of Bingo and can be cast to Bongo as Bongo is Superclass of Bingo...
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

--------------------------------------------------------------------------
Bango b10=(Bongo)b3
--------------------------------------------------------------------------

The meaning of the above statement is this.
Bongo b=(Bongo)b3;

Bango b1=(Bango)b;

Think about this ,if you think the above statement will work then the below statement will also work.
Bango b10=(Bongo)b3.



Thanks

Anil Kumar
 
Brian Deer
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your responses. Sorry I left the semi-colons out of the second half of the example.

brian
 
Arch enemy? I mean, I don't like you, but I don't think you qualify as "arch enemy". Here, try this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic