Static VS. Dynamic Binding in Java

Static Vs. Dynamic Binding in Java

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

Static binding and dynamic binding in Java

Trying to sum up discussion. Edit as required.

Static binding in Java occurs during Compile time while Dynamic binding occurs during Runtime.

At compile time, p1 and p2 both are types of Parent and Parent has doSmth(Object) method hence both lines binds to the same method.

    p1.doSmth("String");
p2.doSmth("String");

During runtime, dynamic binding comes into picture.

p1 is instance of Child1 and Child1 has overridden doSmth(Object) hence the implementation from Child1 is used.

p2 is instance of Child2 and Child2 does NOT override doSmth(Object) hence the implementation of inherited method from Parent is called.

Java: static- vs. dynamic binding (again)

You do not override class variables in Java you hide them.

Overriding is for instance methods. Hiding is different from overriding.

In your case you are hiding the member variable of super class. But after creating the object you can access the hidden member of the super class.

What is the difference between dynamic and static polymorphism in Java?

Polymorphism

1. Static binding/Compile-Time binding/Early binding/Method overloading.(in same class)

2. Dynamic binding/Run-Time binding/Late binding/Method overriding.(in different classes)

overloading example:

class Calculation {  
void sum(int a,int b){System.out.println(a+b);}
void sum(int a,int b,int c){System.out.println(a+b+c);}

public static void main(String args[]) {
Calculation obj=new Calculation();
obj.sum(10,10,10); // 30
obj.sum(20,20); //40
}
}

overriding example:

class Animal {    
public void move(){
System.out.println("Animals can move");
}
}

class Dog extends Animal {

public void move() {
System.out.println("Dogs can walk and run");
}
}

public class TestDog {

public static void main(String args[]) {
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object

a.move();//output: Animals can move

b.move();//output:Dogs can walk and run
}
}

Static Binding and Dynamic Binding

Your example is dynamic binding, because at run time it is determined what the type of a is, and the appropriate method is called.

Now assume you have the following two methods as well:

public static void callEat(Animal animal) {
System.out.println("Animal is eating");
}
public static void callEat(Dog dog) {
System.out.println("Dog is eating");
}

Even if you change your main to

public static void main(String args[])
{
Animal a = new Dog();
callEat(a);
}

this will print Animal is eating, because the call to callEat uses static binding, and the compiler only knows that a is of type Animal.

Difference between Static binding and Dynamic binding of Array

Your static array with dynamic size (called a variable length array, short VLA) only works thanks to a language extension in your compiler. It's a C99 thing, which isn't contained in the C++ standard, meaning it won't be portable.

The other obvious difference is that you can pass the pointer to the dynamic array somewhere else, save it somewhere, return it from a function, or in other words, have it outlive the scope it was created in. You can't do that with static arrays, as they are destroyed at the end of their scope.



Related Topics



Leave a reply



Submit