Java/J2EE and SOA (252 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (46 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (252 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What is Dynamic Binding In Java And How To Use It?

Last updated on Sep 13,2019 88 Views
9 / 10 Blog from Java OOPS

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Dynamic binding in Java is an essential concept that every programmer must be familiar with as it solves various real-time problems. Following pointers will be covered in this article,

Dynamic binding in java is an essential concept that every programmer must be familiar with.
To understand the working of dynamic binding, we must be aware of another type of binding, known as Static Binding.

Moving on with this article on Dynamic Binding in Java

Static Binding

Also known as Early Binding, it is a binding that is resolved by the compiler at compile time.

It must be noted that the binding of private, final and static methods is done during compile time. This is due to the fact that static binding provides a better performance. The compiler is aware that these methods cannot be overridden and are always accessed by the object of local class. Thus, the object of the class(local class) is determined easily by the compiler.

Example:

public class Main
{
public static class superclass
{
static void print()
{
System.out.println("This is the superclass");
}
}
public static class subclass extends superclass
{
static void print()
{
System.out.println("This is the subclass");
}
}
public static void main(String[] args)
{
superclass sup = new superclass();
superclass sub = new subclass();
sup.print();
sub.print();
}
}

In the example given above, the print method of superclass is static, and the compiler is aware that it cannot be overridden in the subclass.
Thus, the output of the code is as follows:

Output:
This is the superclass
This is the superclass

Moving on with this article on Dynamic Binding in Java

Dynamic Binding In Java

In this type of binding, the method to be called is not decided by the compiler. An appropriate example for dynamic binding is Overriding. Here, the parent class and the child class have the same method.

Example:

public class Main
{
public static class superclass
{
void print()
{
System.out.println("This is superclass");
}
}
public static class subclass extends superclass
{
@Override
void print()
{
System.out.println("This is subclass");
}
}
public static void main(String[] args)
{
superclass sup = new superclass();
superclass sub = new subclass();
sup.print();
sub.print();
}
}

Output:

In the example given above, the compiler is unaware about the print that is to be called. The compiler works by referencing variables and not by the type of objects. Due to this, binding is deferred to runtime.
This is superclass
This is subclass

Moving on with this article on Dynamic Binding in Java

Difference between Static and Dynamic Binding

α The static binding is used by private, static and final members. For the virtual methods in Java, the binding is done at the run time in accordance with the run time object.
α While static binding uses Type information, dynamic binding makes use of Objects for binding.
α Static supports Overloading, while Dynamic Binding supports Overriding.

Thus we have come to an end of this article on ‘Dynamic Binding In Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.