• 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

[object cast]..

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding http://www.javaranch.com/ubb/Forum24/HTML/011381.html
We get a conclusion.For object,
Assigning son to parent. //compile and run
Son s = new Son();
Parent p = s;

Casting parent to son. //compile but can't run
Parent p = new Parent();
Son s = (Son) b;
Is the above statement right?? thanks for help.
Liao
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The 2nd should be Son s = (Son) P
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I remember correctly, it will be correct for compile time.
The compiler may allow the cast.
But at run time the execution of the program may depend upon what class that object is referring to.
Hope my english is correct to convey what I intended to..
any comments...Welcome
tvs sundaram
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chao,
That's right.
You can assign a subclass (Son) object to a superclass (Parent) type as it is guaranteed to have all the same behaviour.
You cannot assign a superclass to a subclass because the compiler knows the superclass does not have all the same behaviours and attributes as the subclass (remember, subclasses are specializations of superclasses, they are the same with something extra).
You can cast a superclass (Parent) object to a sublcass type (Son) and the code will compile (the compiler assumes you know what you are doing) but at runtime, if the cast object does not have all the behaviours/states of the declared type, you get an error.
Hope that helps
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good explanation Jane....i enjoyed
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic