File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 9043
    
  10
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Answer to Interview Question
 
Similar Threads
can a default class be subclassed?
Runtime check for methods
cloning
Generics/wildcards in variable declaration
overriding in inheritance (just try & reply)