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.