Guidewire

www.guidewire.com

Interview Question

Consultant Interview

Given a class A and another (B) that inherits from it, if

  you have a method that casts B to A and invokes a method x() that both A and B implement, which class's implementation is invoked?
Answer

Interview Answer

4 Answers

0

Isn't this a trick question since multiple inheritence is not allowed in java?

newb on Aug 29, 2011
1

class B's method will be called.

nobody on Dec 1, 2011
0

nut shell b is called
program i have not compiled and run but should work :)

public class A

{

  public void x()

   {

System.out.println("i am in A");

   }

}

public class B extends A

{

  public void x()

   {

System.out.println("i am in B");

   }

}

public class calling

{

public static void main String args[]

{

A a = new A()

a.x();// a is called

B b = new B()

b.x() // b is called

a=b //casts B to a. allowed

b.x()// //b is called

a.x()/ //b is called

jack on Jan 30, 2012
0

Jack is right. Once executed "a=b" then a.x() method will call x method in class B

SC Chen on Sep 28, 2012

Add Answers or Comments

To comment on this, Sign In or Sign Up.