Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding and if-else order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding and if-else order" Watch "Overriding and if-else order" New topic

Overriding and if-else order

Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
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

Joined: Aug 27, 2008
Posts: 137
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.

Thanks,<br />Srilatha M
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

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.

Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

instanceof operator is to check IS-A relationship.

assume below code.

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


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 ]

SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
I get a feeling that, that was a very silly question :-)
Thanks anyway everyone !!
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
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 ]

Preparing for SCJP 6.0
I agree. Here's the link:
subject: Overriding and if-else order
jQuery in Action, 3rd edition