Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Final class can't be used on left side of instanceof operator ?/

 
vishal mishra
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Namaste Friends !!

why program given below is giving compile time error??




Please Explain.....
Thanks
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java compiler is clever enough to know that E could never be an instance of I, so it doesn't allow that statement.
 
vishal mishra
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:The Java compiler is clever enough to know that E could never be an instance of I, so it doesn't allow that statement.


Thank You Friend !! Got it !!
 
Arnab Sunny Rakshit
Greenhorn
Posts: 19
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the final class implements interface I then compiler will be happy.

In my opinion. Compiler allows only objects and classes in the same hierarchy (if obvious to compiler)to be used with the instanceof operator. I said "obvious to compiler" because compiler allows this --
new ArrayList () instanceof I

 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!