Example & Tutorial understanding programming in easy ways.

What is the difference between static binding and dynamic binding in java?

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

2) private, final and static methods and variables uses static binding and 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

Example of static binding

class Bike

{

private void drive()

{

System.out.println("bike is runnning...");

}

public static void main(String args[])

{

Bike b=new Bike();

b.drive();

}

}

 

Example of dynamic binding

class Vehicle{

void run()

{

System.out.println("vehicle is runing...");

}

             }

class Bike extends Vehicle{

void run(){System.out.println("bike is runing...");

}

public static void main(String args[])

{

Vehicle v=new Bike();

v.run();

}

}

Read More →