answersLogoWhite

0

The virtual method invocation is a little tricky to explain. But I rather show you an example. Suppose you have a super class like this:

public class Super{

public int number = 1;

public String getColor(){

return "red";

}

}

and now we have a subclass of Super:

public class Sub extends Super{

public int number = 2;

public String getColor(){

return "blue";

}

}

All right, so what is the out put of the next program?

public static void main(String[] args){

Super supersub = new Sub(); //You can do this thanks polymorphism

System.out.println( supersub.getColor() + supersub.number );

}

The output could be "blue2" or maybe "red1". Nooo, the real input is "blue1" and this is due the virtual method invocation. Let me explain it better, you are using a variable of the 'Super' type, but you store there a 'Sub' object instead, the output must be either of the two that I predict before, but the java is capable of track in run time the real method implementation that you are using ("the one that return blue") but it can do the same with your object variable.

User Avatar

Wiki User

∙ 16y ago
User Avatar

What else can I help you with?

Related Questions

What is virtual method invocation in java and net?

Virtual method invocation is a term borrowed from C++. It means that methods are invoked polymorphically. Instead of invoking the method in the compile-time type, the method is invoked as its runtime type. In C++, you declare methods virtual that are executed this way.


What is throw exception in java?

The presence of the keywords "throws exception" on a method signature means that, the method may throw an exception whhich it does not handle. It also means that the method that is calling or invoking it has to handle such exceptions. If the calling method does not handle that exception it would have to in turn use the same "throws exception" clause and throw it to its parent method.


What is overridnig method and overlording method in java?

There is no such thing as overlording in Java.


What is Systenoutprintln?

You probably mean "System.out.println", which is a system method in Java that allows you to print text to the screen.


What is the task of the main method in java platform?

It is the method that gets called when a Java application is started.


What is function in java terminology?

In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.


How nesting of methods is done in java?

No, Java only allows a method to be defined within a class, not within another method.


What is a method in Java?

A Java method is a sequence of statements. It is comparable to a function, subroutine, or procedure in other languages.


What is the purpose of NoMatchException handling method in java?

java exception


Why comparator has equals method?

The Java superclass Object says that all Java objects have an equals method. Thus Comparator has an equals method.


What does the RMI mean?

RMI means Remote Method Invocation and it is a way to programm distributed code in Java


What is the meaning of a method in Java programming?

a method is a variable