• Post Reply Bookmark Topic Watch Topic
  • New Topic

identifying the super object which invoked a method which was overridden  RSS feed

 
lakr raj
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class A{

// method1 here

}

class extends B{

// method1 overridden here

}

when the method1 of the b's object is invoked from an object of class A,
is it possibel for the object of B to know which object of A invoked current method.

I am just curious to know if theres any way that we can identify the caller object
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, if I understand you correctly, there would only be one object - an object that is both of type A and type B at the same time.

Second, there probably is a way to do that, but it would be kind of a hack. (A rather unreliable way, for example, would be to examine the stack trace of the current thread.)

In general, your methods should be designed so that they don't care who is calling them. If they did, they'd be closely coupled to their callers, with all kind of nasty effects: less testability, reusability, extensibility...
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have understood your question correctly then you can find the caller method, something like:

The callee will be:



The caller will be:




You can use this approach to implement a chain-of-responsibility design pattern. But I am not sure it is the best way to achieve it.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ak pillai:
If I have understood your question correctly then you can find the caller method, something like:


That's not fully reliable, though, because there is no guarantee that the stacktrace actually is available.

You can use this approach to implement a chain-of-responsibility design pattern. But I am not sure it is the best way to achieve it.[/QB]


I don't understand this part. How would the above help in implementing that pattern???
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not reliably but a SecurityManager can impose some restrictions on how a particular object allows itself to be called.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm - the question was:
... B to know which object of A invoked current method.


which object, not which Method or Class.
So the question would be, how to know ...

... it was c2?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!