Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Fern Sanchez   on May 16 In Java Category.

  
Question Answered By: Huette Miller   on May 16

hope following example helps you .........

class A
{
public void foo()
{
System.out.println("Class A");
}
}

class B extends A
{
public void foo()
{
System.out.println("Class B");
}
}


public class C
{
public static void main(String [] args)
{
A a=new A();
B b=new B();
A ref=null;

a.foo(); // early binding  --- calls method foo() of class A ....
decided at compile time
b.foo(); // early binding --- calls method foo() of class B ....
decided at compile time


ref=b;
ref.foo(); // late  binding --- --- calls method foo() of class B
.... decided at Run time
}

}

Share: