• 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

Reference Variable Casting Fun

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what today's brain twister was for me:

The compiler says:
.......incompatible types
found : A
required: B
B o2 = o1;
^
I'm sure theres a simple answer but I can't find it in K&B: Why does the compiler think that o1 is an "A" and not a "C"?
Thank you again!
Gary
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
o1 is an "A" during compile time and becomes C during run time. compile time comes first, therefore compiler doesn't like it. Should have similar info in the polymorphism chapter.
 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony:
In K&B it says that "All the compiler can do is verify that the two types are in the same inheritance tree...." (page 113).

Now of course A and B are in the same inheritance tree but the compiler still does not like that line of code. Why doesn't the compiler see that o1 ("A") is part of B's inheritance tree? Does the compiler want to see a casting done?
thanks
g
 
Tony Smith
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Why doesn't the compiler see that o1 ("A") is part of B's inheritance tree?"
Actually it doesn't have much to do with seeing part of the inheritance tree; it's more of a fundamental issue on how you are suppose to use the reference variable.


Let's say we have a parent class named animal and child class named cat.

class animal{}
class cat extends animal{}

cat is a animal but
animal is not a cat!!

so you can have

animal a = new cat();

but not cat c = new animal();

Remember to always have reference variable of something in the high inheritance tree and point to some instance in the equal or lower inheritance tree. When you are trying to point to some instance in the higher inheritance tree, 9 out of 10 times the program will blow up if not in compile time, most likely in run time.

In your case it's actually rare that if you do explicit cast, the program will actually run. But it's very rare to see example like this:

A o1 = new C( );
B o2 = (B)o1;

Because it turned into something like B o2 = new C();
during run time which is permitted.

I was thinking more traditional case where you have:

A o1 = new A();
B o2 = (B)o1;

even though it will compile, during runtime it blows up. Just remember pointing downwards in inheritance tree good, but if try to point upwards, then you are asking for trouble.

[ September 25, 2007: Message edited by: Tony Smith ]
[ September 25, 2007: Message edited by: Tony Smith ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic