• Post Reply Bookmark Topic Watch Topic
  • New Topic

example of dynamic binding  RSS feed

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
Bartender
Posts: 4044
47
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
linus dale
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 4044
47
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 59146
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4044
47
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4044
47
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 just had the craziest dream. This tiny ad was in it.
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!