Skip to main content
added 61 characters in body
Source Link
dtech
  • 289
  • 1
  • 7

No, in this case Sub will simply override Sup.go() and so only Sub.go() is executed.

Open recursion is when a method in a class can call itself (i.e. its own signature) which is recursion, but due to dynamic dispatch (can) actually call a subclass, which is open recursion:

class A {
    protected int counter = 1;
    public void loop() {
        System.out.println('Looping ' + counter + ' times');
        counter++;
        this.loop();
    }
}

class B {
    public void loop() {
        if(counter == 1) {
             super.loop();
        }
    }
}
class A {
    protected int counter = 1;
    public void loop() {
        System.out.println("Looping " + counter + " times");
        counter++;
        this.loop();
    }
}

class B extends A {
    @Override
    public void loop() {
        if(counter == 1) {
             super.loop();
        }
    }
}

Now new A().loop() will loop indefinitly*, but new B().loop() will terminate.

* It will actually cause a stack overflow unless you have an inifinite stack or tail-call optimization

No, in this case Sub will simply override Sup.go() and so only Sub.go() is executed.

Open recursion is when a method in a class can call itself (i.e. its own signature) which is recursion, but due to dynamic dispatch (can) actually call a subclass, which is open recursion:

class A {
    protected int counter = 1;
    public void loop() {
        System.out.println('Looping ' + counter + ' times');
        counter++;
        this.loop();
    }
}

class B {
    public void loop() {
        if(counter == 1) {
             super.loop();
        }
    }
}

Now new A().loop() will loop indefinitly, but new B().loop() will terminate.

No, in this case Sub will simply override Sup.go() and so only Sub.go() is executed.

Open recursion is when a method in a class can call itself (i.e. its own signature) which is recursion, but due to dynamic dispatch (can) actually call a subclass, which is open recursion:

class A {
    protected int counter = 1;
    public void loop() {
        System.out.println("Looping " + counter + " times");
        counter++;
        this.loop();
    }
}

class B extends A {
    @Override
    public void loop() {
        if(counter == 1) {
             super.loop();
        }
    }
}

Now new A().loop() will loop indefinitly*, but new B().loop() will terminate.

* It will actually cause a stack overflow unless you have an inifinite stack or tail-call optimization

added 49 characters in body
Source Link
dtech
  • 289
  • 1
  • 7

No, in this case Sub will simply override Sup.go() and so only Sub.go() is executed.

Open recursion is when a method in a class can call itself (i.e. its own signature) which is recursion, but due to dynamic dispatch (can) actually callscall a subclass, which is open recursion:

class A {
    protected int counter = 1;
    public void loop() {
        System.out.println('Looping ' + counter + ' times');
        counter++;
        this.loop();
    }
}

class B {
    public void loop() {
        if(counter == 1) {
             super.loop();
        }
    }
}

Now new A().loop() will loop indefinitly, but new B().loop() will terminate.

No, in this case Sub will simply override Sup.go() and so only Sub.go() is executed.

Open recursion is when a method in a class can call itself (i.e. its own signature), but due to dynamic dispatch actually calls a subclass:

class A {
    protected int counter = 1;
    public void loop() {
        System.out.println('Looping ' + counter + ' times');
        counter++;
        this.loop();
    }
}

class B {
    public void loop() {
        if(counter == 1) {
             super.loop();
        }
    }
}

Now new A().loop() will loop indefinitly, but new B().loop() will terminate.

No, in this case Sub will simply override Sup.go() and so only Sub.go() is executed.

Open recursion is when a method in a class can call itself (i.e. its own signature) which is recursion, but due to dynamic dispatch (can) actually call a subclass, which is open recursion:

class A {
    protected int counter = 1;
    public void loop() {
        System.out.println('Looping ' + counter + ' times');
        counter++;
        this.loop();
    }
}

class B {
    public void loop() {
        if(counter == 1) {
             super.loop();
        }
    }
}

Now new A().loop() will loop indefinitly, but new B().loop() will terminate.

Source Link
dtech
  • 289
  • 1
  • 7

No, in this case Sub will simply override Sup.go() and so only Sub.go() is executed.

Open recursion is when a method in a class can call itself (i.e. its own signature), but due to dynamic dispatch actually calls a subclass:

class A {
    protected int counter = 1;
    public void loop() {
        System.out.println('Looping ' + counter + ' times');
        counter++;
        this.loop();
    }
}

class B {
    public void loop() {
        if(counter == 1) {
             super.loop();
        }
    }
}

Now new A().loop() will loop indefinitly, but new B().loop() will terminate.