2

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1. [What does it mean to “program to an interface”?](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) – OH GOD SPIDERS May 02 '17 at 12:58
  • 1
    If you've worked with interfaces you should see the benefit of using them. A simple example is a List interface in some library and you create an class that implements that List interface, how would the code in that library know about the methods you implement. So they are dynamically looked up at runtime. Compare it to virtual functions in C++ if you know about that. – xander May 02 '17 at 13:00

2 Answers2

2

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Hi @Stephen, would like to know if I create object like below Animal b = new Cat(), will it create only the partial object or complete Cat object.. because through interface object we can call only the interface methods not all the methods in child class.. So i have a thought of whether JVM will create only the object for interface methods? – PrabaharanKathiresan May 02 '17 at 13:22
  • 1
    It creates a complete `Cat`. Not only that, the instance will *always* be a `Cat`. It won't change just because I assigned the `Cat` reference to `Animal`. Indeed, if I tried to cast `a` to a `Dog`, I will get a runtime exception. The JVM *knows* that the instance is a `Cat` ... even though the type of the **variable** is `Animal`. – Stephen C May 02 '17 at 13:25
  • @Stephen thanks for your input and time you took to clarify –  May 02 '17 at 14:32
1

dynamic method resolution means Single method which can be applied to solve multiple problems. Ex: Consider Shape is an interface and has method name draw. you have Rectangle and Circle classes implements Shape Interface. So when you create instance of Rectangle object and call the draw method will draw the Rectangle shape.. In other case you can instantiate Circle instance and call draw method to draw Circle...

In interface you may assign child object in the parent container. Ex: Shape p = new Rectangle();

in this case it will create the instance of Rectangle and assign it into Shape p..

but from the Shape p object you can call only the draw method... you can not call other methods in the Rectangle Object since its assigned to parent interface and parent has only draw method.

PrabaharanKathiresan
  • 1,099
  • 2
  • 17
  • 32