Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

case: static binding? dynamic binding?

I know that overloading uses static binding and overriding uses dynamic binding. But what if they are mixed? According to this tutorial, to resolve method calls, static binding uses type information while dynamic binding uses actual Object information.

So, does static binding happens in the following example to determine which sort() method to invoke?

public class TestStaticAndDynamicBinding {
    
    @SuppressWarnings("rawtypes")
    public static void main(String[] args) {
        Parent p = new Child();
        Collection c = new HashSet();

        p.sort(c);
    }
}

.

public class Parent {
    
    public void sort(Collection c) {
        System.out.println("Parent#sort(Collection c) is invoked");
    }
    
    public void sort(HashSet c) {
        System.out.println("Parent#sort(HashSet c) is invoked");
    }
}

.

public class Child extends Parent {
    
    public void sort(Collection c) {
        System.out.println("Child#sort(Collection c) is invoked");
    }
    
    public void sort(HashSet c) {
        System.out.println("Child#sort(HashSet c) is invoked");
    }
}

ps: The output is : Child#sort(Collection c) is invoked

like image 851
du369 Avatar asked Jun 27 '15 06:06

du369


People also ask

What is dynamic binding?

Dynamic binding or late binding is the mechanism a computer program waits until runtime to bind the name of a method called to an actual subroutine. It is an alternative to early binding or static binding where this process is performed at compile-time.

What is dynamic binding example?

An appropriate example for dynamic binding is Overriding. Here, the parent class and the child class have the same method. Output: In the example given above, the compiler is unaware about the print that is to be called.

Is static binding more reliable or dynamic binding?

In performance, Static Binding is better than the dynamic binding of the methods & variables. Static Binding is also known as Early Binding. As all we know, methods Binding like static, private & final happens over compile time while in the dynamic binding compiler doesn't decide which method will be called.

What is early static binding & Late dynamic binding?

Introduction. Polymorphism allows an object to take multiple forms – when a method exhibits polymorphism, the compiler has to map the name of the method to the final implementation. If it's mapped at compile time, it's a static or early binding. If it's resolved at runtime, it's known as dynamic or late binding.


1 Answers

There are two phases of binding here, as you suspected.

First, the static phase, that will choose to call sort(Collection c). This is done in compile time, and since c is a reference of the Collection type, this method will be used, regardless of the runtime type (which is a HashSet).

Then, in runtime, since p actually contains a Child instance, it's method will be invoked, and you'll get "Child#sort(Collection c) is invoked".

like image 200
Mureinik Avatar answered Sep 22 '22 12:09

Mureinik