Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

invoke a method on object that neither contain nor inherit it, but referenced superclass reference?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is the first time I encountered this situation. I don't no whether it is accepted in programming doctrine?

I created a public class A, then in a new file I created a subclass B that extends A. I add a private method to class A; method doPrivate(). You know that a subclass doesn't inherit anything private from its super-classes.

I add a main method inside the class A. not in a separate class file (As I usually do). Now!

Adding this to the main method:


I know that the main method is inside the class A, so private members are visible inside the class itself. But the strange thing for me is that I send a method to an object that don't know about it. (I know that this method is understood by the reference type since it is of the superclass type)



 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saleh Feek wrote:...But the strange thing for me is that I send a method to an object that don't know about it.


That isn't correct. The Object IS-A B Object, and private method is not visible to Objects of type B, so you can't call doPrivate() on a reference of type B. But the Object you created also IS-An A (and that means it is an A in all respects), and the doPrivate() method would be visible to Objects of type A. Since you have a reference to an A, and because you are calling from inside the A class definition (where the method is visible) then you do have visibility of doPrivate() and can call it.
 
Marshal
Posts: 27375
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Private methods of class A are accessible throughout the code of class A -- that's how those modifiers work. And you said that code was in class A? So that's why it compiles. If that code was part of class B, it wouldn't compile.
 
Saleh Feek
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:

Saleh Feek wrote:...But the strange thing for me is that I send a method to an object that don't know about it.


That isn't correct. The Object IS-A B Object, and private method is not visible to Objects of type B, so you can't call doPrivate() on a reference of type B. But the Object you created also IS-An A (and that means it is an A in all respects), and the doPrivate() method would be visible to Objects of type A. Since you have a reference to an A, and because you are calling from inside the A class definition (where the method is visible) then you do have visibility of doPrivate() and can call it.



Thank you Steve Luke, it is clear now.

Thank you Paul Clapham.
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic