• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

casting of references

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can any one explain me the concept behind explicit casting ; in one of khalids mocks there was a question on casting
class A {}
class B extends A {}
class C extends A {}
public class Q3ae4 {
public static void main(String a[])
{
A x=new A();
B y=new B();
C z=new C();
// insert statement here
}
}
Choices :
(i) x=y;
(ii)z=x;
(iii) y=(B)x;
(iv) z=(C)y;
(v) y=(A)y;
I could not understand why can't we cast like the choice 'iii'
above since y is the reference to the sub class of A
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look
You can downcast only if you've upcasted somewhere.
So,
this will compile all right
y = (B)x
But at runtime it will throw a ClassCastException.
Simply because you cannot cast an Object to something
it never was.
This would run :
A x = new B();
y = (A)x;
Hope this clarifies your doubt!
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pravin,
I think u r making a mistake here this wont compile
A x = new B();
y = (B)x;
please check this is valid at runtime but it wont reach that stage it will be rejected at compile time.
Mohammed, see u r trying to store a super class into a subclass this is not allowed bcoz consider this case
u r deriving sub from super and u have added some extra functions in the subclass and when u call the function using the subclass variable compiler will pass it bcoz it cant checkk the object contained in the variable but at runtime it will give problems and java doesnt allow this thatz the beauty of java.
Hi , Jane Ajith, Bill,mahana dont find u guys in the forum anymore what happened.
Luv Cherry
[This message has been edited by Cherry Mathew (edited January 18, 2001).]
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This involves assignment conversion and explicit casting.
And remember, for assignment conversion of x to y; x must be an instanceof Y (class for object y). If you want to do the other way, you need explicit casting.
For assignment conversion, there is only one check made by compiler. For above example, if you assign y to x, a complier error will occur.
For explicit casting, there are actually two checks. One is made by compiler and one made by runtime. For above example, x = (X) y is okay for compiler b/c y is converted to x by casting. This is syntaxically right to compiler. However, during runtime, another check was made to see if it is possible to convert y to X
and you know you can not (because y is not an instanceof X).
Hope this explains it.

Originally posted by mohammed mustafa:
can any one explain me the concept behind explicit casting ; in one of khalids mocks there was a question on casting
class A {}
class B extends A {}
class C extends A {}
public class Q3ae4 {
public static void main(String a[])
{
A x=new A();
B y=new B();
C z=new C();
// insert statement here
}
}
Choices :
(i) x=y;
(ii)z=x;
(iii) y=(B)x;
(iv) z=(C)y;
(v) y=(A)y;
I could not understand why can't we cast like the choice 'iii'
above since y is the reference to the sub class of A


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic