wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Casting & Conversion of Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting & Conversion of Objects" Watch "Casting & Conversion of Objects" New topic
Author

Casting & Conversion of Objects

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
The following program demonstrates the conversion of an array of objects to legal object ref.types:



It is then stated in my text:

"If line 30 were uncommented then a compilation error would be produced. This is because ITTeacher is not a subclass of EnglishTeacher and the original element type is convertible to the target element type following the rules for class conversion."


Could someone elaborate on what they mean by:
'the original element type is convertible to the target element type'

Thanking you in advance!
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Should this line be

ITTeacher[] temp3 = john;

Essentially the idea is that ITTeacher is a Teacher is a Person

and EnglishTeacher is a Teacher is a Person

But there is no connection between ITTeacher and EnglishTeacher.

An object that is of type EnglishTeacher can be called a Teacher or a Person.

An object that is of type EnglishTeacher can't be called an ITTeacher
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I think this should read, "...the original element type is not convertible to the target element type..."

The original element type is EnglishTeacher, because this is what we actually created with "new."

EnglishTeacher[] john = new EnglishTeacher[5];

Now, an EnglishTeacher is also a Teacher (because EnglishTeacher extends Teacher), and a Teacher is also a Person (because Teacher extends Person), so the following are both valid conversions (i.e., implicit upcasts):

Teacher[] temp1 = john;
Person[] temp2 = john;

However, an EnglishTeacher is not an ITTeacher, so in the following code...

ITTeacher[] temp3 = john;

...the original element type (EnglishTeacher) is not convertible to the target element type (ITTeacher).
[ February 15, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Keith,

Thanks for your reply.

I agree with you that it should be ITTeacher[] temp3 = john; as at present
it would give a compilation error. I think they did this on purpose. Not sure as to why though?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Maureen Charlton:
...it should be ITTeacher[] temp3 = john; as at present it would give a compilation error...

Even as an array, it won't compile. (See my above post.)
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Marc,

That makes sense. Thank you!
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Maureen Charlton:
Quoting a text:
"This is because ITTeacher is not a subclass of EnglishTeacher"
Unless my lunch is affecting my brain, this should read "ITTeacher is not a superclass of EnglishTeacher". If we hadyou still couldn't assign an EnglishTeacher[] to an ITTeacher[]. You can only assign from subtype to supertype with an implicit conversion.

Another thing that's important here is that the important piece is the declaration of the original array -- not the instances it holds. Implicit conversions (i.e. not requiring a cast) only apply to declared types at compile time, not run time instance types.

This example doesn't show it since the declared type and runtime type are the same (EnglishTeacher), but it is important.
[ February 15, 2005: Message edited by: David Harkness ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting & Conversion of Objects
 
Similar Threads
Valiveru's Mock Exam: Question #7
object reference types
Protected members -- Access through inheritance
Seeking Exam Advise
Why call int version,not char version?