Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritence and Typecasting in java  RSS feed

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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 .
 
Ulrika Tingle
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!