I just ran into this question today to determine the output of this Java Program.
package com.senthoor.test;
class Human {
public String message = "NONE";
public Human() {
System.out.println("1");
message = "ONE";
}
public Human(String x) {
System.out.println("2");
message = "TWO";
}
}
public class Student
extends Human {
public Student() {
System.out.println("3");
message = "THREE";
}
public Student(String x) {
System.out.println("4");
message = "FOUR";
}
public static void main(String[] args) {
Human x = new Student("test");
System.out.println(x.message);
}
}
The question followed with asking about Dynamic and Static binding. I think I went though some cross connection in my brain. I have known about these binding, however instead of hooking them up with overloading and overriding, I had hooked them with Constructors and Methods.
I remembered there are instances where static binding is used and there are instances where dynamic bindings are used. Reference type is used(at compile time) to determine which overloaded methods are invoked and instance types (at runtime) are used to determine which methods invoked when methods are overridden. However I completely missed the wicket since the question had a constructor part, and the brain neurons miss fired and some connections were erased and some instant new once were created. These misconnections forced me to a conclusion that with methods, instance type is used and with Constructors reference type is used. It’s funny how our brain works at times?
posted by 88Pro / Tuesday, March 01, 2005