Binding is a mechanism creating link between method call and method actual implementation. As per the polymorphism concept in Java , object can have many different forms. Object forms can be resolved at compile time and run time. If linking between method call and method implementation is resolved at compile time then we call it static binding or If it is resolved at run time then it dynamic binding. Dynamic binding uses object to resolve binding but static binding use type of the class and fields.
Sr. No. | Key | Static Binding | Dynamic Binding |
---|---|---|---|
1 | Basic | It is resolved at compile time | It is resolved at run time |
2 | Resolve mechanism | static binding use type of the class and fields | Dynamic binding uses object to resolve binding |
3 | Example | Overloading is an example of static binding | Method overriding is the example of Dynamic binding |
4. | Type of Methods | private, final and static methods and variables uses static binding | Virtual methods use dynamic binding |
Example of Static and Dynamic Binding
public class FastFood { public void create() { System.out.println("Creating in FastFood class"); } } public class Pizza extends FastFood { public void create() { System.out.println("Creating in Pizza class"); } } public class Main { public static void main(String[] args) { FastFood fastFood= new FastFood(); fastFood.create(); //Dynamic binding FastFood pza= new Pizza(); pza.create(); } }