• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

casting

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is one of Dan Chisholms exam questions #6:

class A {String s1 = "A.s1"; String s2 = "A.s2";}
class B extends A {
String s1 = "B.s1";
public static void main(String args[]) {
B x = new B(); A y = (A)x;
System.out.println(x.s1+" "+x.s2+" "+y.s1+" "+y.s2);
}}

Why does he use a cast here: A y = (A)x; ?
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
upcasting is implicit so no need to do it
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need the cast.

Code compiles and runs fine with or without the cast. Cast should be needed because class A is more general than B.

However if you do the following a cast is required.


[ February 06, 2006: Message edited by: Salvador Cecilio ]
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
......................
class A {String s1 = "A.s1"; String s2 = "A.s2";}
class B extends A {
String s1 = "B.s1";
public static void main(String args[]) {
B x = new B(); A y = (A)x;
System.out.println(x.s1+" "+x.s2+" "+y.s1+" "+y.s2);
}}
......................

What is the o/p??
is it B.s1 A.s2 A.s1 A.s2
 
Michael Carlson
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sethi, you have the correct output.
Thanks guys
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[QB]......................
class A {String s1 = "A.s1"; String s2 = "A.s2";}
class B extends A {
String s1 = "B.s1";
public static void main(String args[]) {
B x = new B(); A y = (A)x;
System.out.println(x.s1+" "+x.s2+" "+y.s1+" "+y.s2);
}}
......................
Here depending on the o/p required u need to do the casting.
For A y = x ( i.e means no casting; y will act as object of B)
So the o/p of y.s1 is "B.s1".
Now if do A y = (A)x (i.e explicit casting; it will be the object of A)
So the o/p of y.s1 will be "A.s1".

Let me correct if I am wrong.

Regards,
Sumit Jain

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the casting here is not necessary.
The compiler always means y is an A object.
For the runtime y point to a B object, so the result must be
B.s1 A.s2 A.s1 A.s2
with or without the casting
 
reply
    Bookmark Topic Watch Topic
  • New Topic