19

Ive saw the next paragraph in some computer science test and i'll hope i can get here a good explanation of what it means because i googled it for an hour and can't find anything..

"When we say Java language has virtual method calling we mean that in java applications the executed method is determined by the object type in run time"

What does it mean? can anyone explain it better?

Popokoko
  • 6,413
  • 16
  • 47
  • 58

4 Answers4

41

The author of these lines used the c++ terminology of virtual.

A better terminology is dynamic binding / dynamic dispatch.

That means, that the object's dynamic type is "chosing" which method will be invoked, and not the static type.

For example: [pseudo code]:

class A {
  public void foo() { }
}
class B extends A { 
  public void foo() { }
}

when invoking:

A obj = new B();
obj.foo();

B.foo() will be invoked, and NOT A.foo(), since the dynamic type of obj is B.

amit
  • 175,853
  • 27
  • 231
  • 333
  • Just a question for clarification: if the foo method in B would have a different signature, e.g. `public void foo(int i) {}` then `A.foo()` would be selected right? – VanTheMan Nov 14 '19 at 07:37
  • 1
    @VanTheMan Yes, this is not overloading it in this case – amit Nov 14 '19 at 22:22
  • 2
    Or actually, do you mean that the method isn't *overriding* in that case? As far as I understand, overloading refers to a method that has the same name, but has different parameters. And *overriding* is when an inherited method has the same name *and* the same signature. – VanTheMan Nov 16 '19 at 15:16
14

we mean that in java applications the executed method is determined by the object type in run time

interface Animal{
  public void eat();
}


class Person implements Animal{
   public void eat(){ System.out.println("Eating Food");}
}



class Eagle implements Animal{
   public void eat(){ System.out.println("Eating Snake");}
}

in main

Animal animal = new Person();
animal.eat(); //it will print eating food
animal = new Eagle();
animal.eat(); //it will print eating snake
jmj
  • 237,923
  • 42
  • 401
  • 438
  • Thanks!! I think i know get it but correct me if im wrong: so about it being said "in java applications the executed method is determined by the object type in run time" it means that java determines the object only when executing? in your example the animal.eat (1st one) will execute the Person's method and on the 2nd it will execute the Eagle's method, is that what it was ment to? – Popokoko Feb 26 '12 at 14:12
  • Exactly, and object will get created run time so it is run time decision – jmj Feb 26 '12 at 14:13
4

Suppose you have a class Fruit, with two subclasses Orange and Banana. And suppose that Fruit has a String getColor() method.

Orange can override the getColor() method to return "orange". Same for Banana, which can have it return "yellow".

When some method uses an object of type Fruit, and call the getColor() method, the method that will be called is Banana.getColor() if the type of the Fruit is in fact Banana.

 private void printColor(Fruit f) {
     System.out.println(f.getColor());
 }

 ...

 Fruit fruit1 = new Banana();
 Fruit fruit2 = new Orange();
 printColor(fruit1); // prints yellow
 printColor(fruit2); // prints orange     
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

Employee.java

public class Employee
{
   private String name;
   private String address;
   private int number;
   public Employee(String name, String address, int number)
   {
      System.out.println("Constructing an Employee");
      this.name = name;
      this.address = address;
      this.number = number;
   }
   public void mailCheck()
   {
      System.out.println("Mailing a check to " + this.name
       + " " + this.address);
   }
}

VirtualMethod.java

class Salary extends Employee
{
   private double salary; //Annual salary
   public Salary(String name, String address, int number, double
      salary)
   {
       super(name, address, number);
       this.salary=salary;
   }
   public void mailCheck()
   {
       System.out.println("Within mailCheck of Salary class ");
       System.out.println("Mailing check to " 
       + " with salary " + salary);
   }

}

public class VirtualMethod
{
   public static void main(String [] args)
   {
      Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);
      Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
      System.out.println("Call mailCheck using Salary reference --");
      s.mailCheck();
      System.out.println("\n Call mailCheck using Employee reference--");
      e.mailCheck();
    }
}

Output

Constructing an Employee
Constructing an Employee
Call mailCheck using Salary reference --
Within mailCheck of Salary class
Mailing check to  with salary 3600.0

Call mailCheck using Employee reference--
Within mailCheck of Salary class
Mailing check to  with salary 2400.0

Explanation

here, we instantiate two Salary objects. One using a Salary reference s, and the other using an Employee reference e.

While invoking s.mailCheck() the compiler sees mailCheck() in the Salary class at compile time, and the JVM invokes mailCheck() in the Salary class at run time.

Invoking mailCheck() on e is quite different because e is an Employee reference. When the compiler sees e.mailCheck(), the compiler sees the mailCheck() method in the Employee class.

Here, at compile time, the compiler used mailCheck() in Employee to validate this statement. At run time, however, the JVM invokes mailCheck() in the Salary class.

User42
  • 970
  • 1
  • 16
  • 27
Bharat
  • 37
  • 11
  • 4
    I know its just an example, but "class Salary extends Employee" is not a modeling. A Salary is not an Employee. – user3711421 Jan 20 '16 at 09:14