ContactUs :

Please send your Questions & Answers or Feedback to "mohan@javabook.org"

Static Binding Example in Java

Here is an example of static binding in java, which will clear things on how overloaded methods in java are bonded during compile time using Type information.

 

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

 

In above example of static binding in Java we have overloaded sort() method, one of which accept Collection and other accept HashSet. we have called sort() method with HashSet as object but referenced with type Collection and when we run method with collection as argument type gets called because it was bonded on compile time based on type of variable (Static binding)  which was collection.

Related Posts Plugin for WordPress, Blogger...
Flag Counter