• 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 Casting Conceptual Problem

 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

class A{}
class B extends A{}

if we do following,

A a=new A();

B b=(B)a;

It compiles fine(same inheritance hierarchy) but a runtime exception (because object type is A reference type is B) know.

if we do following,

A aa=new B();

B bb=aa;

It both compiles(Type A and B in same inheritance hierarchy) and run fine( because reference type and object type are same type which is B)


So my point it is that the theory for object casting if i am not wrong,

1.For compile fine should be in the same inheritance hierarchy
2.And for run fine object type should be only equal to reference type (Is it only equal?)


Thank you.



 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harshana Dias wrote:Hi,

A aa=new B();

B bb=aa;



i think it will not even compile . check it once again.

it needs a cast B bb=(B) aa; will compile fine & run without exception.

avi sinha
 
Harshana Dias
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

avi sinha wrote:

A aa=new B();

B bb=aa;



Opps my mistake..yes it need a cast there..thankx sinha

B bb=(B) aa;

By the way,

Is my points i have understood for a successful casting correct? i mean,

1.For compile fine should be in the same inheritance hierarchy
2.And for run fine object type should be only equal to reference type (Is it only equal?)
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i didn't get your golden points written at the bottom.

what i know is:

1> you can cast object refernces only if they are in the same inheritance tree. you can ,means it will compile .it doesn't say that it will not give any runtime error.

2> if the original object & the referance are of the same type ,it will run fine too.

3>if the original object is an object of the subclass of the referance type ,it will run fine too.


avi sinha
 
Harshana Dias
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

avi sinha wrote:well i didn't get your golden points written at the bottom.

what i know is:

1> you can cast object refernces only if they are in the same inheritance tree. you can ,means it will compile .it doesn't say that it will not give any runtime error.

2> if the original object & the referance are of the same type ,it will run fine too.

3>if the original object is an object of the subclass of the referance type ,it will run fine too.


avi sinha




Thank you very much sinha...that is what i exactly looking for
 
Ranch Hand
Posts: 41
Mac OS X Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Very Much to Harshna Dias and Avi Sinha, Actually the problem is same with me also i also get confuse manytimes In object casting. So just want to bother you guys little bit more. What I under stands by this example and the rules is



I am Not sure if my concept is correct if anything is wrong please you guys rectify it.
Thanks in Advance.
Jeetendra
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jeetendra Choudhary wrote:
B b = (B) a; //can't down cast because not necessarily a ISA B;
[/code]



well jeetendra i can say you are correct but casting has two faces one is wheather it is allowed i.e. it will compile successfully or not another one is it will run without exception or not.

if the referance & object type are in the same inheritance tree,compiler just thinks that there is some possiblity in it that casting is correct hence the above line will compile fine.but at runtime since A IS-NOT a B it will give ClassCastException

avi sinha
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You understand it well
 
Harshana Dias
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jeetendra Choudhary wrote:





B b = (B) a //It compiles fine but runtime fails(ClassCastException)
Note: For compile fine it only checks whether the two types are in same inheritance hierarchy and if so you can cast for super class type like you did here

For run fine either object type i mean "a" should be equal or subclass of reference type (means type B or a should be a subclass of B)..but here object type is Type A which is superclass of B so runtime fails

A aa = new B()// both compiles and runs..basically its implicit casting

B bb = (B)aa // compiles and run fine according what i said above..but wht said here is kind of not understanable. any way keep in mind here that aa refernce type is A not B ok..aa object type is B. got it?

Best Regards Bro.

 
jeetendra Choudhary
Ranch Hand
Posts: 41
Mac OS X Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very very much guys it really helped me regarding my concepts.

Thanks & Regards
Jeetendra.
 
jeetendra Choudhary
Ranch Hand
Posts: 41
Mac OS X Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ummmmmmm Guys Again confused sorry to disturb you again......



Now comes My Confusion the aa is having an object type of B. Due to casting {(B)aa} or due to actually having object type of B {new B()}??
Also if there are some rules like above 3 rules for down casting upcasting follows the simply the 1st rules means it should be in same inheritance tree and equally apply to down casting but what abt 2nd and 3rd rules for downcasting.
 
Harshana Dias
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jeetendra Choudhary wrote:ummmmmmm Guys Again confused sorry to disturb you again......



Now comes My Confusion the aa is having an object type of B. Due to casting {(B)aa} or due to actually having object type of B {new B()}??



due to actually having object type of B-Cast wont change the original reference or object type..it just make sure it fit in to the relavant type
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic