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

2

15

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?

Hip answered 11/7, 2012 at 10:59 Comment(11)
"real time scenario" does not mean what you think it means. What you're interested in is a "real world scenario".Mellifluous
Without this concept you wouldn't have the only feature that makes Java's OOP model worthwhile -- polymorphism.Lacreshalacrimal
see here: https://mcmap.net/q/686305/-java-virtual-methodsMarcos
How do you understand the "Java virtual method invocation" ? Generally speaking virtual methods do not exist in Java.Phyllis
I think we could close as duplicate as knowing what are "virtual methods" in java answers the question.Broomcorn
@MarkoTopolnik, I'm from those people that believe in statement when everything is cool nothing is cool. In Java every method that is not private can override, naturally this possibility was reserved only for those methods that was marked by developer. As it goes for Java thin that runtime-polymorphism (emarcus.org/papers/MPOOL2007-slides-marcus.pdf) describe better what is going on there.Phyllis
@Vash Read through the slides, it doesn't mention Java.Lacreshalacrimal
@Vash: private and static are non-virtual in Java. final methods are non-virtual (unless they themselves override another non-final method). So while it is the normthat a method is virtual, it's definitely not the only option.Mellifluous
@JoachimSauer, You have right, that why I wrote generally speaking at the first place. I'm aware about those "exceptions", so i think in the future i will not write that virtual methods do not exists but the concept of it is really transparent for the developer. Marko I read those slides, i wanted only to show the difference as the concept apply to Java. And thank you for being so accurate even in comments ;-).Phyllis
@MarkoTopolnik I agree with Vash. Java has runtime polymorphism on methods, other than those declared static or final. It doesn't have a keyword or a language concept known as 'virtual', unlike C++ which does.Mormon
@EJP I saw your answer to the other question. Java has the same concept, only uses a different name. The implementations use a vtable. Since this question is not about terminology, but about the concept itself, saying that Java doesn't have virtual methods would be tantamount to saying it doesn't have method dispatch.Lacreshalacrimal
R
24

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.

Rascally answered 11/7, 2012 at 11:7 Comment(4)
If we wanted to print 'B' we would have called B object = new B(); object.getName(); - What makes reference of one Class for a Different Class object special ?Hip
@Rascally Can you give an example (real time scenario) to show the situation explained above.I will be glad to have practical situation where we use them. I have understood your explanationHip
@Meenakshi: This is very useful if we have a method from an external API that demands an A object and that will call getName(). Then you can feed it a B instead, and this way change how it works. This is, as stated by @Vash, called polymorphism, and is extremely useful.Rascally
big thanks for EDIT example, it clarifies concept a lotPhenetole
L
1

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.

Lacreshalacrimal answered 11/7, 2012 at 11:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.