Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding and if-else order

 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a piece of code written by self for experimentation purposes.

If I have the following code, my output is Superclass.



Whereas, if I change the order of mmy if-else clause like this, the output is Subclass.



And if I do not have the "else" keyword and just two if-s, both Superclass and Subclass are printed? Does it mean that the reference "r" is instance of both the sub and super classes? I can understand that polymorphism is into play here, with all the overriding rules of choosing the function. But, any special reasons as to why this behaviour occurs?
 
M Srilatha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instanceof operator checks for IS-A relationship.
That means objects of all subclasses will pass the instanceof test for superclass name.

Here in this code:
r is an instance of Sub class which extends Super. Thats the reason, both expressions r instanceof Sub and r instanceof Super will return true and the print stmt will get executed. And else condition will be skipped.
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you use if - else as in

if(condition) {} //1
else if(condition) { } //2

If the first condition is true, the second if is not executed. The second if is executed only when the first if condition is true.

and when you remove else

Both the if statements are executed.


The keyword else includes a conditional execution.
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instanceof operator is to check IS-A relationship.

assume below code.

class A{}
class B extends A{}
class C extends B{}

here

new B() is instanceof A,B and Object but same will return false with C.
new C() is instanceof C,B,A and Object

just see what actual object it is pointing instead of reference and all classes above hierarchy will pass the instanceof test.
[ September 29, 2008: Message edited by: ramesh maredu ]
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get a feeling that, that was a very silly question :-)
Thanks anyway everyone !!
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No question is silly..
Now you have a better understanding..
Dont hesitate to post silly questions, if they bother you..
Posting a silly question is better than not posting it at all and being in doubt..
[ September 29, 2008: Message edited by: Paul Somnath ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic