Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Vs. Dynamic Binding in Java

I'm currently doing an assignment for one of my classes, and in it, I have to give examples, using Java syntax, of static and dynamic binding.

I understand the basic concept, that static binding happens at compile time and dynamic binding happens at runtime, but I can't figure out how they actually work specifically.

I found an example of static binding online that gives this example:

public static void callEat(Animal animal) {     System.out.println("Animal is eating"); }  public static void callEat(Dog dog) {     System.out.println("Dog is eating"); }  public static void main(String args[]) {     Animal a = new Dog();     callEat(a); } 

And that this would print "animal is eating" because the call to callEat uses static binding, but I'm unsure as to why this is considered static binding.

So far none of the sources I've seen have managed to explain this in a way that I can follow.

like image 456
user2309750 Avatar asked Sep 26 '13 00:09

user2309750


People also ask

What is difference between static and dynamic binding?

The static binding uses Type information for binding while Dynamic binding uses Objects to resolve to bind. Overloaded methods are resolved (deciding which method to be called when there are multiple methods with the same name) using static binding while overridden methods use dynamic binding, i.e, at run time.

Does Java use dynamic or static binding?

Static binding in Java occurs during compile time while dynamic binding occurs during runtime. private , final and static methods and variables use static binding and are bonded by compiler while virtual methods are bonded during runtime based upon runtime object.

What is static binding vs dynamic runtime binding?

Static binding happens when all information needed to call a function is available at the compile-time. Dynamic binding happens when the compiler cannot determine all information needed for a function call at compile-time.

Does Java have static binding?

There are two types of Binding: Static and Dynamic Binding in Java. If the compiler maps the method at compile-time, it is Static Binding or early binding.


1 Answers

From Javarevisited blog post:

Here are a few important differences between static and dynamic binding:

  1. Static binding in Java occurs during compile time while dynamic binding occurs during runtime.
  2. private, final and static methods and variables use static binding and are bonded by compiler while virtual methods are bonded during runtime based upon runtime object.
  3. Static binding uses Type (class in Java) information for binding while dynamic binding uses object to resolve binding.
  4. Overloaded methods are bonded using static binding while overridden methods are bonded using dynamic binding at runtime.

Here is an example which will help you to understand both static and dynamic binding in Java.

Static Binding Example in Java

public class StaticBindingTest {       public static void main(String args[]) {         Collection c = new HashSet();         StaticBindingTest et = new StaticBindingTest();         et.sort(c);     }     //overloaded method takes Collection argument     public Collection sort(Collection c) {         System.out.println("Inside Collection sort method");         return c;     }     //another overloaded method which takes HashSet argument which is sub class     public Collection sort(HashSet hs) {         System.out.println("Inside HashSet sort method");         return hs;     } } 

Output: Inside Collection sort method

Example of Dynamic Binding in Java

public class DynamicBindingTest {        public static void main(String args[]) {         Vehicle vehicle = new Car(); //here Type is vehicle but object will be Car         vehicle.start(); //Car's start called because start() is overridden method     } }  class Vehicle {     public void start() {         System.out.println("Inside start method of Vehicle");     } }  class Car extends Vehicle {     @Override     public void start() {         System.out.println("Inside start method of Car");     } } 

Output: Inside start method of Car

like image 183
Maulik Patel Avatar answered Sep 20 '22 08:09

Maulik Patel