88Pro Thinking

cat /senthoor/mind | grep thought > blog

Tuesday, March 01, 2005

Compile Time Vs Run Time Binding

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() {

message = "ONE";
public Human(String x) {
message = "TWO";
public class Student
extends Human {
public Student() {
message = "THREE";
public Student(String x) {
message = "FOUR";
public static void main(String[] args) {
Human x = new Student("test");

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

This page is powered by Blogger. Isn't yours?

doteasy.com - free web hosting. Free hosting with no banners.