Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am implementing inheritance of something called Contexts. Each Context holds a link to its parent context. If that Context is the root Context, its parent is null.

In order to walk up the inheritance tree and get the data from each Context, I want to do this (in pseudocode)

private Hashtable getDataAsMap() {
    Hashtable map  =  this.parent != null  ?  
                          this.parent.getDataAsMap()  :  
                          new Hashtable();

    // add my data

    return map;


Is this recursion? I'm calling the same method, but on a different object. Does that still count? If not, what would you call it?

share|improve this question
yeah that is recursion, though more specifically here it can be called delegation – ratchet freak Jan 14 '13 at 19:17
It's a recursive call only if this.parent is an object of the same type as this, so the same method is called. I know you said that both objects are instances of Context, but it's conceivable that this or this.parent is a subclass of Context and therefore may have a different implementation of getDataAsMap(). – Caleb Jan 14 '13 at 20:13
up vote 10 down vote accepted

Yes. This satisfies the two criteria for a recursive algorithm:

  1. An algorithm that takes 1 or more parameters and calls itself with a simplified version of the parameter(s) and
  2. A base case that tells the algorithm where to stop recursing.

However, this looks like an odd way to do this. It keeps looking up the tree until finding a root node (a node with no parent), and then returns a new object that's not actually associated with any of the nodes. The end result will be identical to saying return new Hashtable();, but slower. Are you sure that's what you intended?

share|improve this answer
So the fact that it's calling itself on a different object makes no difference? – chama Jan 14 '13 at 19:21
yes - it is. It will return a hashtable with the data from all the nodes in the tree, with each later generation overwriting the data that it shares with its parent. – chama Jan 14 '13 at 19:23
@Chama: The "object it's calling on" is actually a parameter to the function. The compiler essentially changes to bar(foo, 1). It's a basic part of the object-oriented model. So yeah, this is an example of classic recursion; only the syntax is different. – Mason Wheeler Jan 14 '13 at 19:24
@chama the compiler does not "change" anything, it's just syntax. But if it's easier to think about it that way, sure. – Petter Nordlander Jan 14 '13 at 20:09
Hah, actually I was dead wrong. I wrote a diatribe here talking about the definition of the word then went to find a reference for it, and found the english definition which was as I expected, but then I found this definition from math which actually says without rule #2 what you have is circular and for something to be recursive it needs to have base cases that do not self-reference. Learn something new every day :) Add that URL to your answer if you will, it's actually a great expanded explanation of recursion. – Jimmy Hoffa Jan 15 '13 at 14:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.