Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reference Type Casting

 
Srinivas Katta
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have executed the below program


and expected the Compile Time errors will be generated at Lines marked Line 9 and Line 10 as Class A is not implemented Interface I and C is not A. But I see only Line 10 gives the Compile Error not Line 9, Can anybody explain the reason

Thanks
Srinivas
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please QuoteYourSources - tell us where you copied this question from.
 
Srinivas Katta
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this in Exam Lab Practice Exam
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 10 has an error because an object of reference type C cannot possibly be an A.

But line 9 - ask yourself this. Can a reference of type A possible be an I? And consider the following class:
 
Srinivas Katta
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is possibl if we implement the line of code you have given, but that is not included in Original Source. in that Case how Line 9 will not give any compiler Error
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by the way : just make class A as final, and then compile

from java doc:

The detailed rules for compile-time correctness checking of a casting conversion of a value of compile-time reference type S (source) to a compile-time reference type T (target) are as follows:
If S is an interface type:
If T is an array type, then T must implement S, or a compile-time error occurs.
If T is a class type that is not final (§8.1.1), then the cast is always correct at compile time (because even if T does not implement S, a subclass of T might).
If T is an interface type and if T and S contain methods with the same signature (§8.4.2) but different return types, then a compile-time error occurs

 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srinivas Katta wrote:It is possibl if we implement the line of code you have given, but that is not included in Original Source. in that Case how Line 9 will not give any compiler Error

But that's not the point. The compiler will only give an error if that cast cannot possibly work. The compiler doesn't try to look at the previous lines and work out whether it will work - it looks at the types and works out whether it could.

Add my class E, and add the line a = new E(); just before line 9, and the cast will be successful. And because it could be successful, the compiler will allow it (and you'll get a run-time exception when it doesn't work).
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Line 10 has an error because an object of reference type C cannot possibly be an A.

But line 9 - ask yourself this. Can a reference of type A possible be an I? And consider the following class:


This means,
A a = new E(); //possible
I i = new E(); //possible
I i = new A(); //how come it is possible? - class A has no knowledge about the methods in I, nor does it implement it.

Class A has no knowledge about its subclass E too! I'm just a way too confused, i think!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Kumar Kannan wrote:


This means,
A a = new E(); //possible
I i = new E(); //possible
I i = new A(); //how come it is possible? - class A has no knowledge about the methods in I, nor does it implement it.

Class A has no knowledge about its subclass E too! I'm just a way too confused, i think!


Don't you go through the Seetharaman Venkatasamy's post? If S(in your case, interface I) is an interface type, and If T(in you case, class A) is a class type that is not final, then the cast is always correct at compile time (because even if T does not implement S, a subclass of T might(in your case, class E)).

Have a look, on you recent post regarding this scenario in the general forum.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic