• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Answer to Interview Question

 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To whoever posted the interview question:

You wanted to call Test.meth() without using another reference and without changing the signatures.

One response was to use reflection.

Here is another.

I wonder if there is a third answer ...
 
Vincent Wong
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why donot you use polymorphism here??just like this:
Test t=new Test();
t.meth(); //call the method in super class
t=new Test1();
t.meth(); //call the method in sub-class

just my own opinion
 
Marilyn de Queiroz
Sheriff
Posts: 9062
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The original question stated that you could not create a reference to the Test class.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The original question said:

Here how should i call Test's method without using referance
& also the function of both should be same means (return type & parameters both should same)

I took "without using reference" literally and I didn't use t1, the only reference provided.

Of course, any method invocation involves some sort of reference, but my solution did not create a reference variable.

If you take "without using reference" to mean without creating a new reference and you take "the function of both should be same means (return type & parameters both should same)" to mean that you can't change classes Test and Test1, then I think the problem is unsolvable.

We could further discuss what the interviewer quoted by the the interviewee really meant, but that's not a Java discussion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic