Difference between Dynamic binding and message passing in oop?
Message to method bindingA question of whether a message should bind toa method in the class to which the reference variable pointing to the receiver was declared to be an instance of (static binding), ora method in the class to which the receiver is an instance of at run-time (dynamic binding).Dynamic bindingin the context of OOP typically refers to the binding of methods to messagesmethods varying dynamically entails much of the power of the OO approachmain source of power in an OO languagesearch for method (code body) to bind to a message starts from the class to which the receiver currently (i.e., at run-time) is an instance of, and and proceeds up the class inheritance hierarchy from there (static binding initiates the search from the class to which the reference variable pointing to the receiver was declared to be an instance of)if no method found anywhere, a run-time error (method not found) is reported and this is typically the only error in a Smalltalk program ever detected and reportedexample: Mammal m; Cow c; if (user input) m = new Cow; // if static binding used, run method in class Mammal bound to run message here // if dynamic binding used, run method in class Cow bound to run message here m.run else c = new Cow; c.runWhy dynamic binding?`to allow software systems to be more easily extended during both development and maintenance' [COPL] p. 461allows dynamic polymorphism, consider sorting (written in a general way)obviates need for abstract classes (e.g., Fruit class, peel method)while other forms of dynamism in languages tend to be compromise efficiency in the run-time system, dynamic binding involves little overheadDynamic vs. static bindingadvantages of each?disadvantages of each?Message to method binding in languagesSmalltalk dynamic by defaultby sending a message to super we can initiate the search (for the method to bind to the message) in the parent class of the class to which the sender belongs (i.e., like sending a message to self with a different entry point for the search (not quite static binding))Java and Eiffel dynamic by defaultin Java, preface method with final qualifier to prevent any subclass from overriding itin Eiffel, preface method with frozen qualifier to prevent any subclass from overriding itC++ static by default (why?)preface method with virtual qualifier for dynamic binding; the search for the method to bind to the message starts in the class which defines the method prefaced with the virtual qualifier and proceeds down the inheritance hierarchy from there to the class to which the receiver object belongsObjective-C, Modula-3, Python, and Ruby use dynamic binding for all methodsSimula, C# and Ada 95 use static binding by default and support dynamic binding as an optionDynamic binding ambiguityDo not confuse dynamic binding of messages to methods with dynamic allocation and deallocations of objects.Smalltalk: manual dynamic allocation, automatic deallocationJava: manual dynamic allocation, automatic deallocationC++: manual dynamic allocation, manual deallocation