Narrowing Conversion of Class type to Interface type

how and why is the narrowing-reference-conversion "from any class type S to any interface type K, provided that S is not final and does not implement K" allowed or performed in java-language and jvm  ?
vxexpkuma2Asked:
Who is Participating?
 
CEHJCommented:
OK, i'll take your word for it. The above proposition is incorrect, as the following demonstrates:

class X {
      interface K {}
      final class S implements K {}

      public X() {
            K k = new S();
      }
}
0
 
CEHJCommented:
Schoolwork?
0
 
vxexpkuma2Author Commented:
no. trying to understand the basics of java.
0
 
objectsCommented:
> provided that S is not final and does not implement K

S must implement K to be able to cast it to K
0
 
SaMuElCommented:
>> how and why is the narrowing-reference-conversion "from any class type S to any interface type K, provided that S is not final and does not implement K" allowed or performed in java-language and jvm  ?

Trick question, it's not.
As objects says, and CEHJ's code demonstartes, S must implement K for it to be allowed.
It makes no difference if S is final or not. Only if it were abstract.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.