aspose file tools*
The moose likes Beginning Java and the fly likes Answer to Interview Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Answer to Interview Question" Watch "Answer to Interview Question" New topic
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: 9053
    
  12
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Answer to Interview Question