Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of Java virtual method invocation? [closed]

I understand what is java method invocation and have practiced many examples using it.

I want to know what is the practical situation or need of this concept. It would be of great help if anyone could give a real world scenario where it is used and what would happen if this concept would have not been there?

like image 675
Meenakshi Avatar asked Jul 11 '12 10:07

Meenakshi


People also ask

What is virtual method invocation in Java?

Virtual method invocation is the invocation of the correct overridden method, which is based on the type of the object referred to by an object reference and not by the object reference itself. It's determined at runtime, not at compilation time.

Why do we use virtual method in Java?

In Java Every non-static, non-final, and public method is a virtual function. These methods can be used to achieve polymorphism. The methods that can not be used to achieve the polymorphism never be virtual function. A static, final, and private method never be the virtual function.

What is Java invocation?

An invocation is a request that has been prepared and is ready for execution. Invocations provide a generic (command) interface that enables a separation of concerns between the creator and the submitter.

What are virtual calls in Java?

Virtual function in Java is expected to be defined in the derived class. We can call the virtual function by referring to the object of the derived class using the reference or pointer of the base class. Every non-static method in Java is by default a virtual method.


2 Answers

Here is an example. Suppose we have 2 classes:

class A {
    public String getName() {
        return "A";
    }
}

class B extends A {
    public String getName() {
        return "B";
    }
}

If we now do the following:

public static void main(String[] args) {
    A myA = new B();
    System.out.println(myA.getName());
}

we get the result

B

If Java didn't have virtual method invocation, it would determine at compile time that the getName() to be called is the one that belongs to the A class. Since it doesn't, but determines this at runtime depending on the actual class that myA points to, we get the above result.

[EDIT to add (slightly contrived) example]
You could use this feature to write a method that takes any number of Objects as argument and prints them like this:

public void printObjects(Object... objects) {
  for (Object o: objects) {
    System.out.println(o.toString());
  }
}

This will work for any mix of Objects. If Java didn't have virtual method invocation, all Objects would be printed using Object´s toString() which isn't very readable. Now instead, the toString() of each actual class will be used, meaning that the printout will usually be much more readable.

like image 196
Keppil Avatar answered Sep 30 '22 14:09

Keppil


OK, I'll try to provide a simple example. You are writing a method that will fill a caller-supplied list:

public void fill(List l) {
  list.add("I just filled the list!");
}

Now, one caller wants to use a linked list; another one prefers a list implementation based on an array. There will be other callers with even more list implementations that you've never even heard of. These are totally different objects. Propose a solution that achieves this without relying on virtual methods.

Without virtual methods this would mean that the type List would already need to have the method add implemented. Even if you had a subtype ArrayList which had an overridden method, the compiler (and the runtime!) would simply ignore that method and use the one in List. It would be impossible to use different List implementations that conform to the same interface; it would be impossible to reuse that line of code in the method fill since it would work only with the method in the type List.

So you see, the whole idea of type hierarchy wouldn't make a lot of sense; interfaces and abstract classes couldn't even exist. The whole of Java would break down into shards without that one feature of virtual methods.

like image 28
Marko Topolnik Avatar answered Sep 30 '22 14:09

Marko Topolnik