?
Solved

Narrowing Conversion of Class type to Interface type

Posted on 2004-09-10
7
Medium Priority
?
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 500 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

741 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