Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

Can someone tell me what is the difference between static and dynamic binding in Java

1 Answer

0 votes
by (13.1k points)
  1. Static binding occurs during compile time whereas, dynamic binding occurs during runtime

  2. Private, final, and static methods use static binding while virtual methods use dynamic binding.

  3. The static binding uses class information for binding while dynamic binding uses objects for binding

  4. Overridden methods use dynamic binding at runtime while overloaded methods use static methods at compile time.

 These examples might give you an idea about static and dynamic binding.

public class StaticBindingExample

{

 public static void main(String args[]) 

{

 Collection collection = new HashSet(); 

StaticBindingExample sbe = new StaticBindingExample();

 sbe.sort(collection);

 }

 public Collection sort(Collection c)

 {

 System.out.println("Inside Collection sort method");

 return c; 

}

 public Collection sort(HashSet hs)

 {

 System.out.println("Inside HashSet sort method");

 return hs;

 }

 }

Output: Inside Collection sort method

class Animal{

    void sound(){

        System.out.println("Inside Animal class");

    }

}

class Cat extends Animal{

    @Override

    void sound(){

        System.out.println("Inside cat class");

    }

}

public class DynamicBindExample

{

public static void main(String[] args) {

Animal animal=new Cat();

animal.sound();

}

}


 

Output: Inside cat class

Want to learn Java? Check out the Java course from Intellipaat.

Related questions

0 votes
1 answer
asked Feb 17, 2020 in Java by angadmishra (6.5k points)
0 votes
1 answer
0 votes
0 answers
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...