• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Casting relation

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

When i cast Dog object to Human it's fail. But when i cast dog reference to Interfae I then compilation success(though runtime exception).Actually what is the relation between two class hierarchies for interface?
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prasun, Nice of you to notice this.

Casting is working differently for Classes and Interfaces.
i.e., for classes the checking is performed at compile time whereas for interfaces at runtime.

Certainly someone can throw more insight here.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one class can be casted to another class if the first class directly or indirectly extends the second class.
a class can be casted to an interface reference type if the class directly or indirectly implements that interface.
 
Ram Manoj
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prahlad,

I suppose you have overlooked the statement.

for classes(casting) the checking is performed at compile time whereas for interfaces at runtime.
 
author
Posts: 23919
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

Originally posted by Ram Manoj:

I suppose you have overlooked the statement.

for classes(casting) the checking is performed at compile time whereas for interfaces at runtime.



Maybe it was overlooked because it is *not* true. Casting, whether to another class or interface reference is always done at runtime. At compile time, the compiler will also check, but it is "best effort", it will only complain about the casts which are obviously not allowed. For example, if the Dog class had been a final class, then the compiler would had complained, as it would have been obvious that a Dog class can't be casted to that interface.

Henry
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think of references pointing to objects like flow of water. You always flow downwards in the chain. Like henry says, obvious illegal casts are complained about at compile time. Otherwise the compiler trusts that you know what you are doing and it may end up blowing at run time
 
Ram Manoj
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me give an example.



In the code it is understandable that checking at Line1, Line3 can only be determined at Runtime because 'animal' object's type is interface.

But no compiler error is thrown at Line2 where an Interface is used for casting.
A compiler error is thrown at Line4 where a Class is used for casting.

Can anyone explain the behaviour of the code at Line 2, Line 4 at compile time.
 
Henry Wong
author
Posts: 23919
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

But no compiler error is thrown at Line2 where an Interface is used for casting.



Just because the "best effort" check at compile time is not finding line 2 as an error, doesn't mean that the check is not done at compilt time too.

The reason the cast is allowed, is because.... the compiler does not know if a class like this will exist in the future.



This is actually a Dog object that also implements I.

A compiler error is thrown at Line4 where a Class is used for casting.



Just because the compiler is able to find a cast at compile time doesn't mean that it isn't done at runtime.

Try this...



This should be able to get by the compiler. And will throw an exception at runtime.

Summary: The cast check is always performed at runtime. At compile time, it will perform a "best effort" check which doesn't always work.

Henry
 
Henry Wong
author
Posts: 23919
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

This should be able to get by the compiler. And will throw an exception at runtime.



Okay, the second example is bad because it uses an interface. How about this instead?



This should be able to get by the compiler. And will throw an exception at runtime.

Henry
 
Ram Manoj
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou Henry,

That gives me a better understanding of the "best effort" check for casting by the compiler.

Summary: The cast check is always performed at runtime. At compile time, it will perform a "best effort" check which doesn't always work.



At line2,

The reason the cast is allowed, is because.... the compiler does not know if a class like this will exist in the future.

class Collie extends Dog implements I {}





This should be able to get by the compiler. And will throw an exception at runtime.



In both cases example class 'Collie' has been the hero for me.

 
We noticed he had no friends. So we gave him this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic