Difference between static binding and dynamic binding

Solution:

Static Binding Dynamic Binding
Definition of a procedure is related to static binding An example for dynamic binding is activation of a procedure
Declaration of a name for a variable is done to bind statically the variable. Binding of a name can be dynamic bound.
The Scope of the declaration is statically bound. Lifetime of a binding is dynamically bound.

Sample code for static binding is shown below:

Static Binding

public class Shape
{  
  public void getArea()
   {
     System.out.println("Shape Area");
    }  
 
 
public static void main(String args[])
  {  
    Shape shape = new Shape();
   
    shape.getArea();
  }
}

Sample code for Dynamic binding is shown below:

public class Rectangle extends Shape{ 
   
   
  public void getArea()
  {
    System.out.println("Rectangle Area");
   
  }  
   
   
   
  public static void main(String args[])
  {  
   
    Shape shape = new Rectangle();  
   
    shape.getArea();  
  }  
}
0 answers