aspose file tools*
The moose likes Beginning Java and the fly likes Dynamic method invocation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dynamic method invocation" Watch "Dynamic method invocation" New topic
Author

Dynamic method invocation

sandeeep shinde
Greenhorn

Joined: Aug 26, 2009
Posts: 13
Hi ,
Please have a look at the code below.


When I run TestPoly the "In Animal eat" output is dissplayed. But eat() is a private method of class Animal.
Horse class will inherit the callEat() but not eat() method. Now I am using reference of Animal but object of Horse class.
So the method callEat() should be called on Horse class. And as it has not inherited the eat() class it should not run successfully.

Can somebody please explain why it is running successfully?
Raghu Devatha
Ranch Hand

Joined: Feb 10, 2009
Posts: 39
callEat() is public and any reference of class Animal can access it. You may call other method inside callEat() which may be private, that should not effect. eat() will always be available to callEat() as they are defined in the same class. You cannot access private methods directly outside a class. For you its always callEat() nothing to do with eat(), right?


dR
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Think about it. After all private methods are always (indirectly) invoked from outside the class, unless your program is a single class application.

The client calls a public method, the public makes use of private helper methods.

By the way, you might not have noticed it, but your code is an example for a good strategy - 'moving code out of overridable methods'


JDBCSupport - An easy to use, light-weight JDBC framework -
sandeeep shinde
Greenhorn

Joined: Aug 26, 2009
Posts: 13
Sebastian Janisch wrote:Think about it. After all private methods are always (indirectly) invoked from outside the class, unless your program is a single class application.

The client calls a public method, the public makes use of private helper methods.



This is behaving as if I have instantiated an Animal object and called the public method on it.
But it is actually an inherited method.
I am confused about inheritance. What does it actually mean to inherit a method.
Is it something like callEat() method code being copied to the Horse class?
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Sunil:

What happens is that methods of the superclass(es), based on the privacy level, are exposed in subclasses because subclass objects are instances of superclass objects. In your example, an object of type Horse is also an object of type Animal, so the callEat() method is exposed in its API. Because Horse does not override Animal's definition of callEat(), when callEat() is called from a Horse object, it falls back to Animal's definition of callEat(), and so executes Animal's private eat() method.

John.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
What inheritance gives you is a means to augment to or manipulate existing behavior. The true power of it lies in the dynamic casting at runtime.

Consider this code



Animal is the super class or interface of an x amount of animals that there might come.
The trick is that the doAnimalThings method couldn't care less which animal is passed to it, as long as it's of type animal. At runtime, the method of the direct implementer is invoked.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

sunil bhope wrote:This is behaving as if I have instantiated an Animal object and called the public method on it.


And that is precisely true. Your Horse IS-An Animal. So when you instantiate a Horse, you instantiate an Animal. When you call a public method on a Horse, you call a public method on an Animal. That public method in Animal then has access to the private method in Animal.

But it is actually an inherited method.
I am confused about inheritance. What does it actually mean to inherit a method.
Is it something like callEat() method code being copied to the Horse class?

No, the code doesn't get copied. If you want an image of how it works then think of it like a building, each class in the Class Hierarchy has one floor in the building. The different 'rooms' would be the methods and variables (with key-cards so only those with correct permissions are allowed to see the room). Horse is at the 'bottom' of the building, being the actual type of the Object. You call a method on it. If the method is present on the 'bottom floor' then that method is executed from Horse. If not, take the elevator up one floor to Animal and look on the second floor to see if the method exists there. If it does then execute it, if not go one floor higher etc... until you get to Object, which is the top floor. And if the method doesn't exist there then you throw an error.


Steve
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
How My Dog Learned Polymorphism

(I'm out of touch -- where is the link to campfire stories?)
sandeeep shinde
Greenhorn

Joined: Aug 26, 2009
Posts: 13
If I am not wrong whenever a class is instantiated all the superclasses of that class are also instantiated.
And when the method is called on that instance( i.e the Horse class here) the search for the method begins from bottom to top.
The method is exceuted on the instnce of whichever class or superclass the method is found on..

The analogy of building made it pretty clear.

Please correct me if I am wrong.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You are not wrong, as a matter of fact you are correct :-) If I wanted to nit-pick just a little I look at this statement:
sunil bhope wrote:...The method is exceuted on the instnce of whichever class or superclass the method is found on..

There is actually only one instance here (just one building), in the analogy the floors don't represent different instances, just type definitions.
sandeeep shinde
Greenhorn

Joined: Aug 26, 2009
Posts: 13
Steve Luke wrote:
There is actually only one instance here (just one building), in the analogy the floors don't represent different instances, just type definitions.


I think that sums up for me. I think I am on a little firm ground now.
Thanks a lot to all of you for the replies.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic method invocation