• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Question on casting with Interface

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,

Why the below does not throw any compile time error. The Bicycle interface no where relates to Jchq class.

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

Can any one please explain why this does not throw any compile time error.

Thanks,
Ravindra.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there is a suitable or matching typecasting present, the compiler is happy with it.

If you could try the same program after removing the typecasting, it will definitely throw an error.

Eventhough it compiles fine, at the runtime you will definitely get an error because of incompatible object of class which does NOT implement the interface.

Look at the modified program below



  • Step 1:

  • Run as it is. Though it compiles fine because of the proper typecast, it will throw a "ClassCastException" at runtime.

  • Step 2:

  • Comment line 1 and Uncomment line 2. and then compile run.
    Here the compiler itself will throw an error because of no proper typecasting.

  • Step 3:

  • Comment line 1 and 2 and Uncomment line 3. Now compile and run. It will NOT only pass the compilation but also the execution. Because at runtime also the object being passed passes the "IS-A Bicycle" test. So no error! you will get the output "Bicycle Implementation"


    Hope this clears!
     
    Ranch Hand
    Posts: 377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Ravindranath Chowdary:
    Why the below does not throw any compile time error. The Bicycle interface no where relates to Jchq class.

    You must look with the compiler eyes on the code. All what the compiler knows is that you have a reference variable of type Jchq, which can hold an object of Jchq or any of its subclasses. Since the compiler doesn't know whether such subclasses exist and which interfaces they implement, he can't validate the cast.

    In contrast, if you make Jchq final, the compiler knows that there are no subclasses and you get the expected error.
     
    Ranch Hand
    Posts: 558
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi,

    Then why this compiled any way;

    Bicycle b = (Bicycle) new Jchq();

    its direct class instantiation.
    Please let me know.
     
    Rancher
    Posts: 43026
    76
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    There are any number of checks a compiler could perform. In practice, only a limited number of them will be implemented. In this case the compiler's thinking could be "well, the developer told me to disregard what the object is, and to assume that it's a Bicycle, so that's what I'm going to do".
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes, as Ulf said i too think that would be the way. Unless you insist the compiler to disregard the check by providing the explicist typecasting, it would definitely check and throw an error if in case of improper matches.
    reply
      Bookmark Topic Watch Topic
    • New Topic