2012-02-26

Java - Compile Time & Run Time

 

// *** instanceof keyword using
// *** Usage: reference instanceof ClassName
if(h instanceof Salary)
{
 Salary s = (Salary) h;
 s.computePay();
}

// *** Virtual Methods
public class Employee {
 //....
 public void mailCheck() {}
}

public class Salary extends Employee {
 //....
 @override
 public void mailCheck() {}
}

public class SuperDemo {
 public static void main(String[] args) {
  //...
  System.out.println("Instantiating a Salary");
  Employee s = new Salary();
  s.name = "XXX";
  s.address = "";
  
  System.out.println("*** Invoking mailCheck on s ***");
  // compile time refers to Employee Class
  // run time refers to Salary Class
  s.mailCheck();
 }
}

No comments: