• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

identifying the super object which invoked a method which was overridden

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not reliably but a SecurityManager can impose some restrictions on how a particular object allows itself to be called.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
A feeble attempt to tell you about our stuff that makes us money
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic