This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Saleh Feek
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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)



 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Clapham
Sheriff
Pie
Posts: 20966
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic