Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Object Reference Conversion

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code in this executes with errors but the answer differs. This question is from gEs : Java(TM) 2.0.
What happens when the following code executes?
1:class Base{
2:// legal code
3:}
4:class Derived1 extends Base{
5: // legal code
6:}
7:class Derived2 extends Base{
8:// legal code
9:}
10 ublic class Test
11:{
12: static public void main(String [] args)
13: {
14: Base b = new Derived1 ();
15: Derived1 d1 = new Derived1();
16: Derived2 d2 = new Derived2();
17: b = (Base) d1;
18: b = (Base) d2;
}
}
A. Compile time error at line 14
B. Run time error at line 18
C. Compile time error at line 17
D. NO ERRORS

Question Help.
B. Run time error at line 18
b is of type Base and points to d1 and d2 is neither so at run
time it fails
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take the lines,
--------------------------------------
14: Base b = new Derived1 ();
15: Derived1 d1 = new Derived1();
16: Derived2 d2 = new Derived2();
17: b = (Base) d1;
18: b = (Base) d2;
--------------------------------------
14: Base b = new Derived1 (); // This is legal, apply the conversion rule, the new class shuld be a superclass of the old class.
15: Derived1 d1 = new Derived1(); // Legal
16: Derived2 d2 = new Derived2(); //Legal
17: b = (Base) d1; // This is casting is legal , because you hv already assigned Derived1 to Base in Line 14.
18: b = (Base) d2;// Illegal.This attempts a (runtime) cast of Derived2 to Base, whereas Base is already assigned to Derived1.
So you'll get a Runtime Exception.
( This culd hv been legal, if there is statement which says,
Base b = new Derived2 ();
Hope this helps.
-Hema
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !
I have copied your exact code and when I compiled and run this ,I did not get any errors.Casting a subclass to a super class is perfectly legal at compile time and runtime.
kodali
 
Jaishanker Vijayakumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Hema:
Take the lines,


I understand that but try this code it will not give you any errors.
Thanks for the detailed explanation.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


18: b = (Base) d2;// Illegal.
This attempts a (runtime) cast of Derived2 to Base (true), whereas Base is already assigned to Derived1.
What? This makes no sense. "Base" is a class - it can't be assigned to anything. "b" is a variable which currently refers to an instance of Derived1 - but so what? Since b is on the left side of the = sign, it is being reassigned. It doesn't matter what the old value of b is; it's getting a new value. The new value is a reference to an instance of Derived2. This can be legally cast to Base, its superclass, because it's always possible to convert a subclass instance to superclass. In fact this is a widening conversion, which means the cast is not even necessary - the compiler knows that a superclass-type variable can always hold a reference to an instance of a subclass. So this could have been
18. <code>b = d2;</code>
and it would still be legal.

So you'll get a Runtime Exception.
No error occurs, as you'll see if you try to compile & run this code.
( This culd hv been legal, if there is statement which says,
Base b = new Derived2 ();
Irrelevant. b was already declared as type Base, and d2 already references an instance of Derived2, so the above statement is already equivalent to "b = d2;".



[This message has been edited by Jim Yingst (edited August 07, 2000).]
 
Jaishanker Vijayakumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is great thanks for your elaborate reply.
Adding to the explanation...
The condition where it will throw an ClassCastException is when
18: d2 = (Derived2)b;
Since b refers to "Derived1".
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic