This Reference and Dynamic Dispatching in Java

This Reference and Dynamic Dispatching in Java

Table of contents

No heading

No headings in the article.

Java is object oriented language which have pillars which represents the design of object orientation concept.

The four pillars of object-oriented programming are:

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

This is common thing we already know but the question is how this concepts work with each other under hood?

well they relay on two important concept called as this reference and dynamic dispatching.

this reference In java when we call any method of the class using object of that class at than time context of that call is passed implicitly as a parameter of that particular method that is called as this reference (in some language like c++ called as this pointer but java doesn't allow developer to use pointers so here developer can only use concept called reference).

So when any method called by object then implicitly this reference pass, this reference contains all relational information about class, metadata, data, methods details etc. so using this techniques java removes global function concept which is big advantage also java can achieve the abstraction, encapsulation, communication between super-sub classes (i.e inheritance), also runtime decide to call which method at which condition (polymorphism).

Dynamic Dispatching In java each object carry its metadata about type, table of virtual function (v-table). Entry of v-table is points to actual bytecode. so in concept of inheritance when derived class(subclass) created it just copy the addresses of super class methods from the v-table.

When object call any virtual function that time with the help of v-table of both classes particular method got a call. so search start first on derived table if derived class implemented that method then that entry should available in v-table so that derived class method get called else searching continue in base class v-table and base class method got call, so all this process happens at runtime so it called as Dynamic Dispatching.

WIN_20220120_20_16_03_Pro.jpg

To make "dynamic dispatching" happen this reference is use. here this reference of derived class is different and this reference of base class is different so its easier to JVM execute dynamic dispatching.

In Java every object have different this reference but v-table and type descriptor is common for all objects of particular class because both are contains class-level details.

Type Descriptor is contains metadata of the class which have class name, method name, types etc.

This is basic details about this reference and dynamic dispatching which is work for four pillars of OOPs under the hood.

Thanks.