• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Answer to Interview Question

 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic