• PHP
  • jQuery
  • Java IO
  • Servlet
  • JavaScript
  • Struts
  • JSP
  • Core Java
  • RubyonRails

  • <

    Static Binding and Dynamic Binding

    There are two types of binding

  • static binding (also known as early binding).

  • dynamic binding (also known as late binding).

  • Static Binding: In Stataic binding compiler resolved at compilation time.It aslo known as early binding. Binding of all the static, private and final methods is done at compile-time .

    Static Binding Example



    public class NewClass 
    { 
        public static class superclass 
        { 
            static void print() 
            { 
                System.out.println("print in superclass."); 
            } 
        } 
        public static class subclass extends superclass 
        { 
            static void print() 
            { 
                System.out.println("print in subclass."); 
            } 
        } 
    
        public static void main(String[] args) 
        { 
            superclass A = new superclass(); 
            superclass B = new subclass(); 
            A.print(); 
            B.print(); 
        } 
    } 
     

    Output
    print in superclass.
    print in superclass
    

    Dynamic Binding: In Dynamic binding compiler does not decide the method to be called. Overriding is a perfect example of dynamic binding. In overriding both base class and derived classes have same method .


    Dynamic Binding Examples



    public class NewClass 
    { 
        public static class superclass 
        { 
            void print() 
            { 
                System.out.println("print in superclass."); 
            }
        } 
      
        public static class subclass extends superclass 
        { 
            @Override 
            void print() 
            { 
                System.out.println("print in subclass."); 
            } 
        } 
      
        public static void main(String[] args) 
        { 
            superclass A = new superclass(); 
            superclass B = new subclass(); 
            A.print(); 
            B.print(); 
        } 
    } 
    

    Output:
    print in superclass
    print in subclass
    













    © copyright 2017-2022 Completedone pvt ltd.