• 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

Casting reference types.

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I'm somewhat confused with reference type casting. I want to clear things out, so I'll try to explain myself how to cast properly. I hope you'll point out the mistakes.
First, let's consider this two classes:



Let's start with the easy part;

Animal is a superclass of a Dog class, therefore Dog is a subclass of an Animal class. Every subclass is a superclass object, thus class Dog is an Animal. So assigning a subclass reference to a superclass, like so:

works perfectly. However, a can refer only to superclass members, so the only thing we can call is an Animal getName() method. But since the getName() method in Dog overrides the method in Animal, the method returns the value defined in a Dog class. Example:

The output is "Class Dog".
As already said, a can refer only to superclass members so calling any member from a subclass causes a compile-time error.
Conclusions of assigning a subclass reference to a superclass variable:
- can refer only to superclass members,
- overriden methods in a subclass are invoked instead of the ones in a superclass.

Now for the second part;

Every Dog is an Animal, but not every Animal is a Dog. That's why Java does not allow statements like

They cause a compile time error. To avoid this error, one must explicilty cast the superclass reference to a subclass type, like so:

Unfortunatlely, this causes a run-time error, and I'm presuming this is because a superclass does not have an is a relationship with a subclass. The way to check if such a relationship exits is to use an "instanceof" operator. Example follows:

Conclusions of assigning a superclass refrence to a subclass variable:
- superclasses must become subclasses in order to be able to assign them to subclass variables,
- instanceof operator helps to determine if a variable can be cast to some arbitrary type T.

Could you please point out the mistakes I made and mention other important facts I may have forgot? Also, if you don't mind, any example where the superclass must be cast to subclass in order to work properly will be greatly appreciated.

Thanks,
Ted.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everything that I read appears to be correct. Although one subtle correction should be made


They cause a compile time error. To avoid this error, one must explicilty cast the superclass reference to a subclass type, like so:
view plaincopy to clipboardprint?

Unfortunatlely, this causes a run-time error, and I'm presuming this is because a superclass does not have an is a relationship with a subclass. The way to check if such a relationship exits is to use an "instanceof" operator. Example follows:



This is not quite the case. The relationship between super and sub types is not stored in this sense. I believe there is NO formal idiom used by Java itself to make a relationship of Dog and Animal other than the following:

Not



If you were to try the following: the compiler would dertermine that an incovertible types error has occurred.

 
Get out of my mind! Look! A tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic