• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between cast to Interface and cast to class

 
Jamoba Black
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I notice that there is a difference between cast to interface and cast to class without a IS-A relationship
As shown below:


Don't understand what reason cause it.
Could anybody give some hits?
Thanks!
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you search first, you may quickly find an answer to your question.

You may find this thread useful.
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple rule: you cant cast class to class if not possible relation between class.




For interface you can cast not final class to interface ever if there is no relation

not final class can have descendant that may implement any interface. and you can assign to variable of type your class object that is descendant and implements interface.

also you may interesting of
5.5. Casting Conversion
especially: 5.5.1. Reference Type Casting

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
5.5.1. Reference Type Casting
Given a compile-time reference type S (source) and a compile-time reference type T (target), a casting conversion exists from S to T if no compile-time errors occur due to the following rules.

S - Type from you cast, T - type to which you cast.
If S is a class type:
If T is a class type, then either |S| <: |T|, or |T| <: |S|. Otherwise, a compile-time error occurs.

This - must be relation or S - is child of T or T is child of S

Furthermore, if there exists a supertype X of T, and a supertype Y of S, such that both X and Y are provably distinct parameterized types (§4.5), and that the erasures of X and Y are the same, a compile-time error occurs.
If T is an interface type:
If S is not a final class (§8.1.1), then, if there exists a supertype X of T, and a supertype Y of S, such that both X and Y are provably distinct parameterized types, and that the erasures of X and Y are the same, a compile-time error occurs.
- this for generic (not for this exam)

If T is an interface type and S is not a final class (i merge sentence from start and next sentence)
, the cast is always legal at compile time (because even if S does not implement T, a subclass of S might).



If S is a final class (§8.1.1), then S must implement T, or a compile-time error occurs.


same logic applied to other sentence from jls - read and try it

 
Jamoba Black
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergej Smoljanov wrote:5.5.1. Reference Type Casting
Given a compile-time reference type S (source) and a compile-time reference type T (target), a casting conversion exists from S to T if no compile-time errors occur due to the following rules.

S - Type from you cast, T - type to which you cast.
If S is a class type:
If T is a class type, then either |S| <: |T|, or |T| <: |S|. Otherwise, a compile-time error occurs.

This - must be relation or S - is child of T or T is child of S

Furthermore, if there exists a supertype X of T, and a supertype Y of S, such that both X and Y are provably distinct parameterized types (§4.5), and that the erasures of X and Y are the same, a compile-time error occurs.
If T is an interface type:
If S is not a final class (§8.1.1), then, if there exists a supertype X of T, and a supertype Y of S, such that both X and Y are provably distinct parameterized types, and that the erasures of X and Y are the same, a compile-time error occurs.
- this for generic (not for this exam)

If T is an interface type and S is not a final class (i merge sentence from start and next sentence)
, the cast is always legal at compile time (because even if S does not implement T, a subclass of S might).



If S is a final class (§8.1.1), then S must implement T, or a compile-time error occurs.


same logic applied to other sentence from jls - read and try it



Thank you Sergej for the details!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jamoba Black wrote:Don't understand what reason cause it.
Could anybody give some hits?

There are indeed a few subtle difference between casting to an interface, casting to a class and casting to a final class.

These threads contain all valuable information (with code snippets to illustrate rules and possible pitfalls):
  • Why will this throw Class Cast Exception?
  • Casting
  • Not sure why my answer on overloading was incorrect
  • Some doubts about casting
  • instanceof operator with an interface versus class


  • (Note: casting and the instanceof operator are closely related, so the rules are exactly the same)

    Hope it helps!
    Kind regards,
    Roel
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic