Dynamic And Static Binding In Java

Posted By : Panaru Chaudhary | 31-Jan-2018

There is the following binding :-

1. Static Binding : -

The binding which can be decided at compile time by compiler is known as static binding

We can achieve static binding by declaring the methods as final, static or private.

 
 How can we say that by declaring like that can achieve static binding?

The reason is compiler knows that all such methods cannot be overridden.

we will always be accessed by an object of same class.

Hence compiler doesn’t have any difficulty to determine the object of class That’s the reason binding for such methods is static.

Let's have an example : 

                
 public class StaticBinding extends  StaticBinding1
    {
        public static void print()
        {
            System.out.println("print in subclass");
        }
        
        public static void main(String[] args)
        {
            StaticBinding1 A = new StaticBinding();  
           //here always called the parent class  print method    
            A.print();       
         
        }
    }
class  StaticBinding1 
{    
       public static void print()
        {
            System.out.println("print in superlass.");
        }    
}   
        

2. Dynamic Binding

In case of Dynamic binding compiler does not  decide which method at compile time which method will be called

Only  decide after object creation if we create a parent class instance then it's method called  but if you create the child 

the class object then it's method will be called, method overriding is the best way to achieve the dynamic binding

In overriding both parent and child classes have the same method, let's have an example:


 public class DynamicBinding extends  DynamicBinding1 
    {
       public void print()
        {
            System.out.println("print in subclass");
        }
        
        public static void main(String[] args)
        {
        	DynamicBinding1  A = new DynamicBinding();  
           // here will called the sub class method .it's depend on which class object being created
            A.print();       
         
        }
    }
class  DynamicBinding1 
{    
       public  void print()
        {
            System.out.println("print in superlass.");
        }    
}    
        
Request for Proposal

Recaptcha is required.

Sending message..