Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is dynamic method resolution

Tags:

java

I am currently reading Herbert Schildt "Java the Complete Reference" and there he has used a term "Dynamic method resolution" and has provided a little explanation, but i am not getting the full import of it so asking for help in this forum.

while discussing 'interfaces', what he is saying is, dynamic method resolution helps in resolution of method name at run-time and it is achieved by declaring a interface variable and using it to refer to a class object. i.e

        interface i = new object();

now what is so unique about it? you can use a class variable also to refer to the same object like:

        class c = new object(); 

so, what is the use of interface here? and why introduce this new term "dynamic method resolution"??

  1. Second he makes a point by saying: " when we use an interface variable to refer to instance of any class, and when you call a method through these interface variables, the method to be executed is looked up dynamically at run time allowing classes to be created later than the code which calls method on them. The calling code can dispatch through an interface without having to know anything about the callee".

    Now, Anything dealing with objects has to be in run-time as objects are created at runtime, Now, I dont understand what he meant by "allowing classes to be created...on them".

Any help will be appreciated.


1 Answers

Here is a little example:

public interface Animal {
    public String sound();
}

public class Cat implements Animal {
    public String sound() { return "meow"; }
}

public class Dog implements Animal {
    public String sound() { return "woof"; }
}

public class Test {
    public static void main(String[] args) {
        Animal a;
        if (args.length > 0)
            a = new Cat();
        else {
            a = new Dog();
        }
        System.out.println(a.sound());  // prints "MEOW" or "WOOF"
    }
}

What is so unique about it? You can use a class variable also to refer to the same object

Yes. But you cannot use a single class variable to refer to an instance that can be an instance of any class that implements the interface.

In Test class, if I declared a to have type Dog or Cat there would be no way to get the code to compile. Without the ability to declare Animal a, I would need to have two distinct variables, and two separate print statements.

This is what dynamic method resolution (aka polymorphism) gives you.


To understand his second point:

public class Test2 {
    public static void main(String[] args) {
        Animal a = PetShop.buyPet(args);
        System.out.println(a.sound());  // prints "MEOW" or "WOOF"
    }
}

The Test2 class will work with my Cat and Dog class from above. It will also continue to work without recompilation if in 3 years time I implement a Goldfish class and modify my PetShop class to stock aquatic pets. And indeed, it is even possible to implement the PetShop class so that it doesn't need to be changed or recompiled to support other kinds of pets.


Now, these examples are clearly not practical. However, the Java features that they illustrate are useful in real Java applications. Indeed, a program as simple as a classic "hello world" program relies on dynamic method lookup.

like image 104
Stephen C Avatar answered May 24 '26 10:05

Stephen C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!