• 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

Reference Casting

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[CODE]
class A{}
class B extends A{}
class C extebds A{}
public class Test{
public static void main(String args[]){
A x=new A();
B y=new B();
C z=new C();
// insert styatement here
}
}
1- x=y;
2- z=x;
3- y=(B)x;
4- z=(C)y;
5- y=(A)y;
which one cause runtime exception
[CODE]
Answer is 3 but anyone explain all options kindly
thanks in advance

 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you encounter such code it's advised to replace A,B and C with real-life things to help understand the relationships among them. I choose that A is Fruit, B is Orange and C is Apple.
Now let's show the relationships: We have that an Orange is a Fruit and an Apple is a Fruit as well.
We create one instance of each, i.e. we have one Fruit instance, one Orange instance and one Apple instance.
Let's look at the different options we have:
1. x=y means that you want to reference an Orange instance with a Fruit reference which is perfectly legal since an Orange is_a Fruit. No runtime exception here.
2. z=x means that you want to reference a Fruit instance with a Apple reference. This will result in a compile-time error since without explicitely casting x to Apple it is not possible and moreover x has to reference an Apple beforehand.
3. y=(Orange)x means that you want your Fruit instance (x) look like it is a Orange and reference it with a variable of type Orange. Well, this will work only if x already references an Orange instance which may not be the case since x may as well reference an Apple instance and you cannot make a Apple look like an Orange. In this case, x references a Fruit and not an Orange. The cast allows to go through compilation without error but at compile time a ClassCastException will be thrown since a Fruit cannot be cast to an Orange.
4. z=(Apple)y means that you want your Orange instance (y) look like it is an Apple and that's is clearly impossible since an Apple and an Orange are not related. This results in a compilation error.
5. y=(Fruit)y means that you want your Orange instance look like it is a Fruit and reference it with a variable of type Orange. The rigth part is not the problem but the result of casting y to Fruit results in y being of type Fruit. Then you want to reference it with a variable of type Orange, which is not permitted by the compiler and results in a compile time error.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by farrukh mahmud:
[B]
[/B]


x=y;
compiles: because B extends A the variable y 'is a' A
z=x;
Compiler error: The variable z is to hold a reference to an C object, because x refers to an A object and an A object is not a C object it fails.
y=(B)x;
Compiles ok becasue of the cast, but will cause a runtime exception because the variable x refers to an A object and can't be cast to a B object.
z=(C)y;
Compiler error: The compiler knows that the classes B and C are not related so it wont let you cast from one to the other.
y=(A)y;
Compiler error: The cast of the variable y to an A object is ok becasue a B object 'is a' A object. but the variable y can only hold a B object or one of its subclasses so trying to put an A object into it fails.
hope this helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
farrukh mahmud
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys explanation is good added by knowlwdge
------------------
Regards
Farrukh Mahmud
 
reply
    Bookmark Topic Watch Topic
  • New Topic