GeekInterview.com
Series: Subject: Topic:
Question: 22 of 1118

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

Code
  1. interface X{
  2.         void m1();
  3.         void m2();
  4. }
  5.  
  6. class Child2 extends Object implements X {
  7.  
  8.         public void m1(){
  9.                 System.out.println("Child2 M1");
  10.         }
  11.         public void m2(){
  12.                 System.out.println("Child2 M2");
  13.         }
  14.        
  15. }
  16. public class ParentChildInfterfaceDemo {
  17.        
  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.         }
  25.  
  26. }
  27.  
Copyright GeekInterview.com

will this code work and if yes then why is x.equals(x1) work? Can you please explain?
Asked by: kool.bird9 | Member Since Jul-2011 | Asked on: Nov 10th, 2011

View all questions by kool.bird9   View all answers by kool.bird9

Showing Answers 1 - 1 of 1 Answers
bholanath das

Answered On : 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 version.so 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.

Yes  1 User has rated as useful.
  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.