• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Doubt in overrididng

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have doubt in following code snippet as the output is chirp chirp, can any one expain why?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think this is overriding anything?
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raghavan Muthu:
What was happening? any inputs guys?

You are able to delete your posts.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic