Solved

Narrowing Conversion of Class type to Interface type

Posted on 2004-09-10
7
161 Views
Last Modified: 2010-03-31
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  ?
0
Comment
Question by:vxexpkuma2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12029638
Schoolwork?
0
 

Author Comment

by:vxexpkuma2
ID: 12030079
no. trying to understand the basics of java.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 12030287
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
 
LVL 92

Assisted Solution

by:objects
objects earned 125 total points
ID: 12031775
> provided that S is not final and does not implement K

S must implement K to be able to cast it to K
0
 
LVL 2

Expert Comment

by:SaMuEl
ID: 12032182
>> 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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now