• 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

Inheritence and Typecasting in java

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am having a query .


I am having a query here why doesn't

Dog = Animal is not posible (subclass= Parent)

I know that with typecast it is possible
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ravi Kiran V wrote:I am having a query here why doesn't

Dog = Animal is not posible (subclass= Parent)




A Dog is an Animal. Not all Animals are Dogs.

Henry
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A Dog is an Animal. Not all Animals are Dogs.



still in some sort of confusion .Can anybody please help me on this .
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ravi Kiran V wrote:

A Dog is an Animal. Not all Animals are Dogs.



still in some sort of confusion .Can anybody please help me on this .



The core issue here is typesafety. If a reference assignment can be guaranteed to work properly already at compilertime it's said to be typesafe. If you have to wait until runtime before you know if it was okay it's not typesafe.

Now an upcast (from subtype to supertype) is always typesafe, whereas a downcast (from supertype to subtype) is not. This is why the compiler will force you to put in an explicit cast in the downcast situation. The compiler says:

"Hey, I cannot guarantee the typesafety of this downcast at compiletime. So I require that you take on full responsibility for it by putting in an explicit cast. Still I won't take your word for it. If I detect at runtime that you lied to me I'll throw a bad cast exception at you."

This is an example of a bad downcast. Say Dog and Cat both are subtypes of Animal:


The compiler will allow the downcast at compiletime because for all it knows it could be okay and you promise it is okay by putting in the explicit cast. At runtime it will be detected that the Animal variable really didn't hold a Cat object like you said it would but a Dog object. A Dog object cannot be assigned to a Cat variable so your program will end in an exception.

Just because downcasts aren't typesafe they should be avoided like the plague. You should downcast only as a last resort.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"Hey, I cannot guarantee the typesafety of this downcast at compiletime. So I require that you take on full responsibility for it by putting in an explicit cast. Still I
won't take your word for it. If I detect at runtime that you lied to me I'll throw a bad cast exception at you."



Hey thank you for this line .
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well to be frank I dont understand this .



This type of TypeCast is valid at compile Time . But at Runtime this will rise to a TypeCastException .



If this is the case why did Java Specification allowed this kind of behaviour ??

Can anybody give me an example at what cases this TypeCast will be ever useful ??

My question would be what is the use of Explict Cast like this ??

Thanks in advance .
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic