• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Reference Type Casting

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please QuoteYourSources - tell us where you copied this question from.
 
Srinivas Katta
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this in Exam Lab Practice Exam
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Montana has cold dark nights. Perfect for the heat from incandescent light. Tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic