Can a reference variable of Interface calls and equals method of Object call? If so, why?

  1. interface X{

  2.         void m1();

  3.         void m2();

  4. }


  6. class Child2 extends Object implements X {


  8.         public void m1(){

  9.                 System.out.println("Child2 M1");

  10.         }

  11.         public void m2(){

  12.                 System.out.println("Child2 M2");

  13.         }


  15. }

  16. public class ParentChildInfterfaceDemo {


  18.         public static void main(String[] args){

  19.                 X x = new Child2();

  20.                 X x1 = new Child2();

  21.                 x.m1();

  22.                 x.m2();

  23.                 x.equals(x1);

  24.         }


  26. }


will this code work and if yes then why is x.equals(x1) work? Can you please explain?

Questions by kool.bird9   answers by kool.bird9

Showing Answers 1 - 1 of 1 Answers

bholanath das

  • Dec 19th, 2011

yes, x.equals(x1) will work .even though the reference is type of interface type but the object is type of child2 which is referred by this reference variable and this Child2 class does not have the equals() method .so it will invoke the equals() method of Object class a calls to x.equals(x1) will produce a Boolean value false.because x1 is pointing to one object in the heap an x2 is pointing to another object in the heap.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.


Related Answered Questions


Related Open Questions