• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

compile time vs runtime error

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys,

have seen this piece of code which had the q. is it compile time or runtime error.

interface Bicycle{
public void Bicycle();
}
public class Jchq {
public static void main(String argv[]){
Jchq j = new Jchq();
Bicycle b = (Bicycle) j;
}
}

The answer was runtime error, but the real question is why not compile time error. Since the compiler can very well see the lack of inheritance right there, it doesn't complain.

Please explain.
RJ
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is that it is legal at compile-time to cast a reference to an interface reference, unless the original reference refers to a final class. The reason is that if the class is not final and it doesn't implement the interface, it might have a subclass that does implement the interface.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See also this thread:
https://coderanch.com/t/264718/java-programmer-SCJP/certification/cast

with an example.

Bu.
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic