Friday, March 5, 2010

Java Puzzle of the day

class A
{
void sayHello()
{
System.out.println("Parent speaking");
}

void getname()
{
sayHello();
}
}

class B extends A
{
@Override
void sayHello()
{
System.out.println("child speaking");
}
}

public class C extends B
{
public static void main(String[] args)
{
B b = new B();
b.getname();
}
}

What will be the output ?

2 comments:

  1. child speaking

    will be the output

    :)

    ReplyDelete
  2. Child speaking is correct...

    the reference variable b holds the reference to object of class B ...so at run time it will call method of class B...

    ReplyDelete