| Author |
Doubt in overrididng
|
Divya Gehlot
Ranch Hand
Joined: Sep 10, 2006
Posts: 238
|
|
Hi, I have doubt in following code snippet as the output is chirp chirp, can any one expain why?
|
SCJP1.5(81%)<br />SCDJWS(94%)<br />next mission SCEA(but need to wait or that)
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
|
Why do you think this is overriding anything?
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
There is no overriding involved here. Should there be overriding, then you should have got chirp, hello as the o/p. Remember always, static methods are not overridden. They are just redefined!
|
SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
Hi nik,
Lastly remember that sub reference variable = sub object super = super super ref variable = sub object but never super ref variable = sub object
I think your last statement should have been "but never sub ref variable = super object" right?
|
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
how come i saw there was a post by "nik" for which only i replied. But once i am done with my post, the original post by "nik" vanished. What was happening? any inputs guys?
|
 |
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
|
|
Originally posted by Raghavan Muthu: What was happening? any inputs guys?
You are able to delete your posts.
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
Thats fine Manfred. I am aware of that. But how come the post to which i replied, was vanishing? I cannot delete others post, i m sure!! One possibility i can think of is, the original poster "nik" would have deleted his own post. Is there any other possibilities?
|
 |
Divya Gehlot
Ranch Hand
Joined: Sep 10, 2006
Posts: 238
|
|
Hi, "but never sub ref variable = super object" I got the thing but my code we are referring super ref variable=subobject and that s possible Can any please elaborate on this . Thanks in advance
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
Hi Divya As static methods are not overridden but just re-defined in subclasses (if present) you may think that the appropriate version of the method present in the corresponding subclass object should get invoked. Just to support, in your example, you are invoking the not-overridden-but-just-redefined talk() method via the subclass instance (Parrot). But during invocation the compiler just sees the "reference type" and NOT the "actual instance" being referred. Here, the instance varaiable of Parrot is being assigned to the superclass array variable "birds". So while invoking, the compiler sees the reference or class type of "birds" which is of type "Bird" so it invokes the method present in the class Bird. Had it been the case of assigning the objects to the subclass array say, It would invoke the talk() method present in the Parrot class because the talk() method is invoked on the instance "p" which is of type "Parrot". If you look at the array elements, the first element is not allowed as its of type Bird which is the superclass because the subclass does not know anything about the superclass. Thats why, the line was saying
but never sub variable = super.
HtH. [ July 09, 2007: Message edited by: Raghavan Muthu ]
|
 |
 |
|
|
subject: Doubt in overrididng
|
|
|