• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

private method resolution

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Maybe my doubt is repetitive but am genuinely stumped.
See the following code


Now according to me the output should be S.printsS1 S.printsS2..but actually it turns out to be R.printsS1 S.printsS2. Now my doubt is that the call to a private method is resolved at compile time depending on the type of reference.The type of reference is s.So shouldnt the private method in S be called which hides the one in R?
[ October 06, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. In my opinion, I don't think there is no override relationship between 2 printS1() mothods because they are private.
 
jaman tai
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I am sorry. Typing mistake. I mean there is no relationship between the private methods.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
private and static methods (also class instance member, no matter what access modifier it has) are "early binding" at compile time. Other methods are "late binding" and calls to these methods are resolved at run time, which enables the so called "Polymorphism".

In your code, R.printS1()is a private method, so it is binded at compile time. R.printS2() is protected, so "Polymorphism" is applicable.

When you invoke s.printS1S2(), R.printS1S2() is invoked. It then invoke printS1()which is binded to R.printS1() at compile time, and invoke S.printS2() due to Polymorphism.

That's it. Hope it helps, mate.
 
anjali desh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But s.printS1S2() means the inherited printS1S2() is called .and then since the type of reference is s ,the private method of S should be called.
Dont know why this is confusing me!!!
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this....



Hope this helps.
 
anjali desh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much Lalitha....!!
The answer is SPOT-ON and there is absolutely no confusion left now.
I appreciate the time and effort spent by you all in clarifying my doubt so soon!!
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that was truly a great reply for the doubt good work !!!
thaxn from mw too i was watching closely on the thread!!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic