Skip to content

Instantly share code, notes, and snippets.

@invkrh
Created September 2, 2015 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save invkrh/0654f97b8ee6e333ef8b to your computer and use it in GitHub Desktop.
Save invkrh/0654f97b8ee6e333ef8b to your computer and use it in GitHub Desktop.
Show that recursive call need to be final or private (avoid overriding)
class C {
def fact(n: Int, result: Int): Int ={
println("super")
if(n == 0) result else fact(n - 1, n * result)
}
}
class C2 extends C {
override def fact(n: Int, result: Int): Int = {
println("sub")
2 * super.fact(n, result)
}
}
println((new C).fact(5, 1))
println((new C2).fact(3, 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment