| Author |
Answer to Interview Question
|
Mike Gershman
Ranch Hand
Joined: Mar 13, 2004
Posts: 1272
|
|
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 ...
|
Mike Gershman
SCJP 1.4, SCWCD in process
|
 |
Vincent Wong
Greenhorn
Joined: Apr 14, 2004
Posts: 1
|
|
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
Joined: Jul 22, 2000
Posts: 9033
|
|
|
The original question stated that you could not create a reference to the Test class.
|
JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
|
 |
Mike Gershman
Ranch Hand
Joined: Mar 13, 2004
Posts: 1272
|
|
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.
|
 |
 |
|
|
subject: Answer to Interview Question
|
|
|