Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance and recursion

Suppose we have the following classes:

class A {      void recursive(int i) {         System.out.println("A.recursive(" + i + ")");         if (i > 0) {             recursive(i - 1);         }     }  }  class B extends A {      void recursive(int i) {         System.out.println("B.recursive(" + i + ")");         super.recursive(i + 1);     }  } 

Now lets call recursive in class A:

public class Demo {      public static void main(String[] args) {         A a = new A();         a.recursive(10);     }  } 

The output is, as expected counting down from 10.

A.recursive(10) A.recursive(9) A.recursive(8) A.recursive(7) A.recursive(6) A.recursive(5) A.recursive(4) A.recursive(3) A.recursive(2) A.recursive(1) A.recursive(0) 

Let's get to the the confusing part. Now we call recursive in class B.

Expected:

B.recursive(10) A.recursive(11) A.recursive(10) A.recursive(9) A.recursive(8) A.recursive(7) A.recursive(6) A.recursive(5) A.recursive(4) A.recursive(3) A.recursive(2) A.recursive(1) A.recursive(0) 

Actual:

B.recursive(10) A.recursive(11) B.recursive(10) A.recursive(11) B.recursive(10) A.recursive(11) B.recursive(10) ..infinite loop... 

How does this happen? I know this is a devised example, but it makes me wonder.

Older question with a concrete use case.

like image 263
raupach Avatar asked Dec 30 '15 14:12

raupach


People also ask

What is recursion and example?

Recursion means "defining a problem in terms of itself". This can be a very powerful tool in writing algorithms. Recursion comes directly from Mathematics, where there are many examples of expressions written in terms of themselves. For example, the Fibonacci sequence is defined as: F(i) = F(i-1) + F(i-2)

What do you mean by recursion?

Definition of recursion 1 : return sense 1. 2 : the determination of a succession of elements (such as numbers or functions) by operation on one or more preceding elements according to a rule or formula involving a finite number of steps.

What is recursion in an algorithm?

Contents. A recursive algorithm is an algorithm which calls itself with "smaller (or simpler)" input values, and which obtains the result for the current input by applying simple operations to the returned value for the smaller (or simpler) input.

What is a good example of recursion?

A classic example of recursion For example, factorial(5) is the same as 5*4*3*2*1 , and factorial(3) is 3*2*1 .


1 Answers

This is expected. This is what happens for an instance of B.

class A {      void recursive(int i) { // <-- 3. this gets called         System.out.println("A.recursive(" + i + ")");         if (i > 0) {             recursive(i - 1); // <-- 4. this calls the overriden "recursive" method in class B, going back to 1.         }     }  }  class B extends A {      void recursive(int i) { // <-- 1. this gets called         System.out.println("B.recursive(" + i + ")");         super.recursive(i + 1); // <-- 2. this calls the "recursive" method of the parent class     }  } 

As such, the calls are alternating between A and B.

This doesn't happen in the case of an instance of A because the overriden method won't be called.

like image 87
Tunaki Avatar answered Sep 30 '22 09:09

Tunaki