aspose file tools*
The moose likes Beginning Java and the fly likes invoke a method on object that neither contain nor inherit it, but referenced superclass reference? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "invoke a method on object that neither contain nor inherit it, but referenced superclass reference?" Watch "invoke a method on object that neither contain nor inherit it, but referenced superclass reference?" New topic
Author

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

Saleh Feek
Greenhorn

Joined: Sep 19, 2012
Posts: 14
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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

Joined: Sep 19, 2012
Posts: 14
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: invoke a method on object that neither contain nor inherit it, but referenced superclass reference?