aspose file tools*
The moose likes Beginning Java and the fly likes example of dynamic binding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "example of dynamic binding" Watch "example of dynamic binding" New topic
Author

example of dynamic binding

linus dale
Ranch Hand

Joined: Jul 01, 2009
Posts: 44


I read dynamic binding happens when the class of the object that calls the method is not known till runtime

can someone give an example of such a situation
thanks
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3837

I read dynamic binding happens when the class of the object that calls the method is not known till runtime can someone give an example of such a situation


Here is a good explanation to clarify your doubts.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
linus dale
Ranch Hand

Joined: Jul 01, 2009
Posts: 44


Dog is subclass of Animal
my doubt is,finally,
doggie is object of class Dog or of class Animal ?

if doggie is an object of Dog,
doggie.show will obviously call show method of Dog and not Superclass Animal
(because of method overrriding,if I am not wrong)
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3837

linus dale wrote:Dog is subclass of Animal my doubt is,finally, doggie is object of class Dog or of class Animal ?

It's an object of type Dog.

if doggie is an object of Dog, doggie.show will obviously call show method of Dog and not Superclass Animal
(because of method overrriding,if I am not wrong)


Yes. That's exactly what happens here.

When you say



It creates an object of type Dog which is referenced by a reference of type Animal called doggie.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39386
    
  28
It is also an object of the Animal class.

This shows dynamic binding, but also shows the hazards of hiding fields. You can get confused because both Dog and Animal classes have a "type" field.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3837

Campbell Ritchie wrote:This shows dynamic binding, but also shows the hazards of hiding fields.


Yes. But that's just to show polymorphic behavior is not allowed for member variables .
linus dale
Ranch Hand

Joined: Jul 01, 2009
Posts: 44
Campbell Ritchie wrote:It is also an object of the Animal class.

This shows dynamic binding, but also shows the hazards of hiding fields. You can get confused because both Dog and Animal classes have a "type" field.




also i wanted to know what all the signature of a method includes

name of method,no. of arguments,data type of arguments
and return type(I am not sure about return type)
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3837

also i wanted to know what all the signature of a method includes


Method should have a name/identifier(optionally with list of formal parameters),return type of the method (if no Type then should be void) and optionally method modifiers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: example of dynamic binding