You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@harmony.apache.org by rohit hitnalikar <ro...@gmail.com> on 2006/01/17 11:31:22 UTC

JVM Spec Interpretation Question

Hello Gurus,

  I am not able to understand the following bit from JVM spec v2.0
which talks about permitted narrowing reference conversions:

  *From any class type S to any interface type K, provided that S is not
final and does not implement K. (An important special case is that
there is a narrowing conversion from the class type Object to any interface
type.)
*
  Can somebody please let me know how to correctly interpret this?
  In what cases, do we come across the need to use such a feature ? an
example would be most helpful.

  This bit is taken from the following links :


http://java.sun.com/docs/books/vmspec/2nd-edition/html/Concepts.doc.html#32879

  or here


http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25379

thanks in advance,
Rohit

Re: JVM Spec Interpretation Question

Posted by Rick McGuire <ri...@gmail.com>.
rohit hitnalikar wrote:
> Hello Gurus,
>
>   I am not able to understand the following bit from JVM spec v2.0
> which talks about permitted narrowing reference conversions:
>
>   *From any class type S to any interface type K, provided that S is not
> final and does not implement K. (An important special case is that
> there is a narrowing conversion from the class type Object to any interface
> type.)
>   
Grrr, sorry about the mostly empty reply before...keystroke problems in 
Thunderbird.

Ok, here's a simple case:

public class Base {
....
}

public interface AnInterface {
...
}

public class SubBase extends Base implements AnInterface
{
....
}

Base a = new SubBase();

AnInterface b = (AnInterface)a;


At the point of the cast, the compiler is unable to determine if 
reference "a" is an object that implements AnInterface.  The base class 
Base does not, but a superclass might, so this narrowing must be allowed.

However, if Base was final, then it cannot have subclasses and thus this 
narrowing cannot succeed.

> *
>   Can somebody please let me know how to correctly interpret this?
>   In what cases, do we come across the need to use such a feature ? an
> example would be most helpful.
>
>   This bit is taken from the following links :
>
>
> http://java.sun.com/docs/books/vmspec/2nd-edition/html/Concepts.doc.html#32879
>
>   or here
>
>
> http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25379
>
> thanks in advance,
> Rohit
>
>   


Re: JVM Spec Interpretation Question

Posted by Rick McGuire <ri...@gmail.com>.
rohit hitnalikar wrote:
> Hello Gurus,
>
>   I am not able to understand the following bit from JVM spec v2.0
> which talks about permitted narrowing reference conversions:
>
>   *From any class type S to any interface type K, provided that S is not
> final and does not implement K. (An important special case is that
> there is a narrowing conversion from the class type Object to any interface
> type.)
> *
>   Can somebody please let me know how to correctly interpret this?
>   In what cases, do we come across the need to use such a feature ? an
> example would be most helpful.
>   
Not terribly well worded, but here's a simple example of this.

public class Base
{
}


>   This bit is taken from the following links :
>
>
> http://java.sun.com/docs/books/vmspec/2nd-edition/html/Concepts.doc.html#32879
>
>   or here
>
>
> http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25379
>
> thanks in advance,
> Rohit
>
>