answersLogoWhite

0


Best Answer

Dynamic binding makes it possible for objects to behave polymorphically even when we do not know the runtime type of the object.

Consider the following:

struct A {

int x;

int data() const { return x; }

};

struct B {

int y; int data() const { return y; }

};

Given that objects of type A and B are not of the same type, how can we pass just one instance of either type to the same function? One way would be to pass two separate references:

void foo (A& a, B& b) {

// ...

}

The problem with this is that we must have two objects to refer to whenever we call this function. If we only have one then we must instantiate a dummy argument for the other. That's hardly intuitive. And how would we differentiate the dummy from the actual argument? We could use pointers instead of references, passing nullptr for the unused argument, but that only serves to make it even less intuitive; a caller might easily pass nullptr for both arguments!

We could use function overloading and create two separate functions:

void foo (A& a) {

// ...

}

void foo (B& b) {

// ...

}

That's certainly more intuitive from the caller's point of view, but it's now a nightmare for the person who has to maintain this code in future. Every time we make a new class for this function we must define a completely new overload to handle it. The ideal here is to have just one function with one implementation.

We could use a void pointer.

void foo (void* p) {

// ...

}

That's certainly an improvement, but we still risk passing a nullptr. If we want to guard against this we must pass a reference, but we cannot declare a reference of unknown type. And the maintenance nightmare still exists because we still have to determine the actual runtime type before we can dereference the pointer.

Let's look at those type definitions again.

struct A {

int x;

int data() const { return x; }

};

struct B {

int y;

int data() const { return y; }

};

Although they are completely different types, they both have a common method, data(). We can exploit that fact by percolating this "pure" interface into a common base class:

struct Base {

virtual int data() const = 0;

virtual ~Base() {}

};

struct A : Base {

int x;

int data() const override { return x; }

};

struct B : Base {

int y;

int data() const override { return y; }

};

Now we can define our function in terms of this one common, abstract base class:

void foo (Base& base) {

int z = base.data();

// ...

}

Problem solved! Now we can derive as many different classes as we want from Base and the foo() function will work as expected without any changes. Note that the function makes no reference whatsoever to the actual runtime type. This is because it doesn't need to know the runtime type, it only needs to know the interface defined in the one and only class that it does need to know about, the Base class!

Note that since the Base class has no data, there is no memory overhead:

assert (sizeof(A)==sizeof(B)==sizeof(int));

The only real overhead is in the virtual tables which is where dynamic binding comes in. When we invoke the Base::data() virtual method, that method is dynamically bound to either A::data() or B::data(), depending on which specific class we passed to the foo() function.

Note also that when we declare a virtual method, we must also declare the destructor virtual as well. This ensures that the most-derived class is always destroyed first, even if we only hold a pointer to its base class:

Base* p = new A {42};

// ...

delete p; // OK -- A is destroyed before Base due to virtual destructor.

Again, virtual destruction is an example of dynamic binding.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago
  • User-modelling and interface customization. Applications I develop tend to be targeted to very specific audiences. I sometimes provide an initial survey or dialog box that lets the user customize the interface. I've been able to develop a suite of interface components that are easy to customize at run-time by switching their ancestors. If, for example, the user is a Spanish-speaking women who is a computer expert, the application can change the ancestor of on-screen text objects to the Spanish Language ancestor and change the ancestors of other interface objects to the Expert ancestor. If at some time in the presentation the user would like more help, the application can switch ancestors to Normal mode, and then switch back to Expert mode later.
  • Preservation of object properties. Object properties persist across changes in mode, movies, etc. I've found this feature very useful for returning to a stable state after a user-initiated digression.
  • Extensibility. The boss says we need a French language version by 5 pm. No problem. I've already isolated language specific behavior in a Spanish Language ancestor. I copy it, change the Spanish to French, add a line of code to switch the ancestors to the French version and voila!

Of course, all this can be done with globals and nested if-then statements too. I've found this approach easier to maintain and much, much easier to extend and customize.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Both possible.

In Java static binding is binding at compile time while dynamic binding is binding at runtime.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How dynamic binding is useful in oops?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is Difference between dynamic polymorphism and static polymorphism with example?

Static polymorphism is used the concept of early binding or we can say compile time binding where as dynamic polymorphism used the concept of late binding or run time binding.


Why does C plus plus allows static binding and not dynamic binding?

Dynamic binding, or late binding, is when the object code for the class does not get loaded into memory until it is needed. This saves time at module load time, at the cost of delayed execution for the first invocation.


Which process of assigning a value to an attribute at the run time is?

Dynamic binding


Advantages of dynamic binding?

There is Static binding and also Dynamic binding. The compiler has to choose from one or the other. Stactic binding defines the properties of the variables at compile time. Therefore, they can't be changed. In dynamic binding the properties of the variables are determined at runtime. Since the variables can change in form, dynamic binding is more flexible but slower. A great example is given in wikipedia it states: Suppose all life-forms are mortal. In object oriented programming, we can say that the Person and Plant classes must implement the Mortal interface, which contains the method die(). Persons and Plants die in different ways; for example, Plants do not have hearts that stop. Dynamic binding is the practice of figuring out which method to invoke at runtime. For example, if we write void kill(Mortal m) { m.die(); } it's not clear whether m is a Person or a Plant, and thus whether Plant.die() or Person.die() should be invoked on the object. With dynamic binding, the m object is examined at runtime, and the method corresponding to its actual class is invoked.


Which binding in C plus plus is preferred and why?

There is no preference as such. The type of binding you use is more dependant upon the design and circumstance rather than any preference you may have. Static binding is certainly more predictable and therefore easier to program, but dynamic binding offers much greater flexibility.

Related questions

When dynamic binding approach in oops?

at runtime


What is binding in oops?

Binding is defined as the connection between the function call and the corresponding program code to be executed. There are two types of bindings. They are; 1.static binding and 2.Dynamic binding.


Explain the concept of late binding and dynamic binding with the help of at least two examples?

the concept of dynamic linking and dynamic binding with example


What are different static and dynamic features of oops?

static feature are aspects of a program that are fixed at compile time dynamic feature can change at run time the static and dynamic is manifested in oo language in number of diff ways.we consider 1.static and dynamic typing 2." " " classes 3." " " method binding


Static vs dynamic binding in c plus plus?

Static binding occurs at compile time. Dynamic binding occurs at runtime.


Why dynamic binding is not possible for normal C functions?

Dynamic binding is certainly possible for normal C functions. Binding is a function of the binder (linker) and has nothing to do with the language itself.


What is dynamic binding in java?

Dynamic Binding means declaring variables at run time only rather than declaring it at compile time.


Is late binding and dynamic binding related to polymorphism?

Late binding and dynamic binding are related to runtime polymorphism. By contrast, compile time polymorphism is known as static binding. Template functions and classes are examples of static binding because the exact type can be determined at compile time.


What is Difference between dynamic polymorphism and static polymorphism with example?

Static polymorphism is used the concept of early binding or we can say compile time binding where as dynamic polymorphism used the concept of late binding or run time binding.


Why does C plus plus allows static binding and not dynamic binding?

Dynamic binding, or late binding, is when the object code for the class does not get loaded into memory until it is needed. This saves time at module load time, at the cost of delayed execution for the first invocation.


What is the other name for polymorphism?

It is also called 'Dynamic binding of Function'


Which process of assigning a value to an attribute at the run time is?

Dynamic binding