This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Assertion  RSS feed

 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

have a look at this piece of code

boolean b1 = new AssertionError() instanceof Exception;
System.out.println("b1 = " + b1);

the above lines doesn't compile.Why???
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Girish,

The code doesn't compile due to same reason as I explained here.

In summary, sibling classes in the inheritance tree cannot be compared with instanceof operator.

Please take a look at the link above I provided.
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Girish Nagaraj,

Both Exception, Error are subclasses of Throwable, AssertionError in turn subclasss of Error.

COming to your question, for application of instanceof operator it is necessary that its left and right operands have subclass, superclass relationship, as in your question there is no subclass superclass relationship between object of AssertionError and Exception, compile time error occurs.

HOpe you got the point
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thanks ppl.

I Got It.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!