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

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

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

Last updated on Sep 13,2019 6.1K Views

14 / 14 Blog from Java OOPS

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.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 27th April,2024

27th April

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

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

edureka.co