• 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

How to call the overridden method?

 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
How do you force the class C1 to call its own method m1() without changing anything in the class C2?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alton Hernandez:
How do you force the class C1 to call its own method m1() without changing anything in the class C2?


Make the method m1 private in class C1. But now I ask you, why does that make it work?
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Corey McGlone:

Make the method m1 private in class C1. But now I ask you, why does that make it work?


But then m1(), in that case, is not an overridden method. C2's m1() is just another method of the same signature.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't. That's the whole idea of polymorphism. You are not constructing a C1 object, you are constructing a C2 object. Since C2 has overridden m1, the m1 in C2 is the m1 that must be used. If C1 wants to always use its own method then it should make m1 final and not let it be overridden in the first place.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C1() {m1();}
From a mechanical (as opposed to conceptual) point of view, at run-time, the target reference (JLS 15.12.4.1) of the method m1 in the constructor C1 is �this�.
The dynamic method lookup process (JLS 15.12.4.4) begins with the actual run-time class of the target reference.
The target reference is not used if the invocation mode is static or nonvirtual (i.e. private).
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic