Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

infinite loop problem  RSS feed

 
John Cebedo
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the following code generates an infinite loop:

abstract class A {
public static A xyz(List<String> list) throws someError {
if (B.aaa(list)) // the problem is in here
return B.xyz(list); // infinite loop from "if" to "return"
if (...)
return ...;
return ...;
}

// abstract method here but not part of the problem
}

abstract class B extends A {
public static boolean aaa(List<String> list) {
return C.aaa(list) || Z.aaa(list);
}
}

abstract class C extends B {
public static boolean aaa(List<String> list) {
return D.aaa(list) || E.aa(list);
}
}

class D extends C {
public static boolean aaa(List<String> list) {...}

public static A xyz(List<String> list) throws someError {...}

// abstract method implemented here
}

in class A, the problem is that i meant to use the xyz() method of class D but the program stubbornly uses class A's xyz() method instead. am i missing something here?
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,

Your problem is that the xyz method in class A is calling herself via the inherited method in class B. Not of class D! As in the line following the problem line you return B.xyz(list);
If the expression in the problem line is true, a stack overflow occurs.

The structure of your example is the same as in this simpler example:



Yours,
Bu
 
John Cebedo
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah, yes! thank you!

it works now. by overriding the xyz() for each of the subclasses above class D and having each of them call the xyz() of a child class, i can now call class D's xyz() through class B's xyz().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!