Javatpoint Logo
Javatpoint Logo

Virtual functions and runtime polymorphism

A member function that has the keyword virtual used in its declaration in the base class and is redefined (Overridden) in the derived class is referred to as a virtual function. The late binding instruction instructs the compiler to execute the called function during runtime by matching the object with the appropriately called function. Runtime Polymorphism refers to this method.

  1. No matter what kind of reference (or pointer) is used to invoke a function, virtual functions make sure the right function is called for an object.
  2. Their primary purpose is to implement runtime polymorphism.
  3. In base classes, functions are declared using the virtual keyword.
  4. Runtime resolution of function calls is carried out.

Polymorphism is a term used to describe the capacity to assume several shapes. If there is a hierarchy of classes connected to one another by inheritance, it happens. Polymorphism, which is defined as "showing diverse traits in different contexts," can be summarised as "showing different characteristics in a variety of situations" and "polymorphism."

What is the use of virtual functions?

To achieve Runtime Polymorphism, virtual functions are primarily used. Only a base class type pointer (or reference) can enable runtime polymorphism. A base class pointer can also point to both objects from the base class and those from derived classes.

Also, without even knowing the type of derived class object, we can use virtual functions to compile a list of base class pointers and call any of the derived classes' methods.



Virtual functions and runtime polymorphism

What are the rules for virtual functions?

  1. Virtual functions are not permitted to be static or friendly to other classes.
  2. Pointers or references of base class type are required to access virtual functions.
  3. Both the base class and any derived classes should use the same function prototype.
  4. There cannot be a virtual constructor in a class. However, it might have a virtual destroyer.
  5. The base class always defines them, and the derived class redefines them.

What is runtime polymorphism?

Runtime polymorphism is the process of binding an object at runtime with a capability. Overriding methods is one way to implement runtime polymorphism. At runtime, not at compilation time, the Java virtual machine decides which method to invoke. Additionally known as dynamic binding or late binding. The parent class's method is overridden in the child class, according to this concept. The term "method overriding" refers to the situation where a child class implements a method specifically that was supplied by one of its parent classes. You can see runtime polymorphism in the example that follows.



Virtual functions and runtime polymorphism

How do virtual functions work?

As mentioned below, the compiler performs two tasks if a class has a virtual function.

  1. A virtual pointer (VPTR), which points to the class's VTABLE, is added as a data member of the class if an object of that class is created. A new virtual pointer is added to the class as a data member for each new object that is created.
  2. VTABLE, a static array of function pointers, is a member of the class regardless of whether an object is generated or not. Each virtual function within that class's class contains the class address in this table's cells.

What are the limitations of virtual functions?

  1. Slower: The virtual mechanism causes the function call to take a little longer, making it harder for the compiler to optimize as it is unsure which function will be called at compilation time.
  2. Virtual functions can make it slightly more challenging to determine where a function is being called from in complicated systems, which makes them more challenging to debug.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA