chevron_left
275 points
10 4 4

In Java,  to resolve binding, the Dynamic binding uses Objects where as Static binding uses Type information. In general, method overloading based on different signatures so that it refers to static binding where method overriding based on different implementation so that it refers to dynamic binding.

Static Binding

The binding which can be resolved at compile time through compiler is called static or early binding. Binding of all of the static, non-public and very last methods is executed at compile-time.

For example:

public class NewClass {
	public static class BaseClass {
		static void print()
		{
			System.out.println("print in BaseBlass.");
		}
	}
	public static class DerivedClass extends BaseClass {
		static void print()
		{
			System.out.println("print in DerivedClass.");
		}
	}

	public static void main(String[] args)
	{
		BaseClass A = new BaseClass();
		BaseClass B = new DerivedClass();
		A.print();
		B.print();
	}
}

In practice, reader can change the reference of object B to Derived class and then check the output. So, in both cases print method of the Base class is called.

Dynamic Binding

In Dynamic binding compiler doesn’t determine the method to be called. Overriding is an excellent example of dynamic binding. In overriding each base and derived classes have same method with same signature.

For example:

public class NewClass {
	public static class BaseClass {
		void print()
		{
			System.out.println("print in BaseClass.");
		}
	}

	public static class DerivedClass extends BaseClass {
		@Override
		void print()
		{
			System.out.println("print in DerivedClass.");
		}
	}

	public static void main(String[] args)
	{
		BaseClass A = new BaseClass();
		BaseClass B = new DerivedClass();
		A.print();
		B.print();
	}
}

Hence methods are not static in this code, so that the output differs for both object A and B.

2 Comments

Good post about static binding.your articles are always full of knowledge. Thank you so much.
Easy to understand. Well described

More Posts

Static binding and Dynamic binding in C++ Lalit Kumar - Apr 28, 2020
What is Dynamic Binding? iamdpunkr - May 16
What is static binding and dynamic binding offonime - Jun 4
Difference between static and dynamic in java Sanjana Sagar - May 29
Event binding on dynamically created elements javascript Sanjana Sagar - Jun 20
Event binding on dynamically created elements javascript Sanjana Sagar - May 20
What is classes and objects in java usmanhashmi - Aug 11
What is failfast and failsafe in java usmanhashmi - Aug 7
What is the difference between public, protected, package-private and private in Java? Alex Samson - Apr 6, 2020
static methods and classes in JAVA Takele - Aug 17