Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

instanceof with interfaces

Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to understand why there is a compatibility error when checking against an interface type


Master Rancher
Posts: 3701
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference is that the Integer class is final, but Animal is not.  That means that the compiler knows that variable "io" can only point to an Integer, not to any other subtype of Integer that implements Driveable.  In comparison, "animal" can point to an instance of an class that extends the Animal class - it's possible that someone could create a class that extends Animal and implements Driveable.  Even if no such class exists at the moment.  The general rule is that if you're classing from a final class to an interface, and the final class does not implement the interface, that's illegal at compile time.  But if the class is not final, it's legal at compile time, and then  you get a run-time exception if the actual class at run-time does not implement the interface.

See JLS 5.1.1. Allowed Narrowing Reference Conversion - the relevant part is

A narrowing reference conversion exists from reference type S to reference type T if all of the following are true:


One of the following cases applies:

  • [...]
  • 5. S is an interface type, T is a class type, and T does not name a final class.
  • 6. S is an interface type, T is a class type, and T names a final class that implements the interface named by S.
  • [...]

  • [...]

    Posts: 70684
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Look in the JLS =Java® Language Specification. It tells you that instanceof won't compile if the cast to the same type won't compile. Since Integer doesn't implement that interface, neither a cast nor instanceof would compile. The cast won't compile because the compiler can tell that it would never succeed.
    Don't call an Integer io because people might confuse that with a moon round a giant planet or input/output. Try i as a name. Don't use the Integer(int) constuctor; use valueOf() or straight boxing instead.
    I will suppress my every urge. But not this shameless plug:
    Thread Boost feature
      Bookmark Topic Watch Topic
    • New Topic