简体   繁体   中英

Java: recursion within main-class calls subclass-method instead of its own method

Example:

class MainClass {
    public doIt() {
        ...
        else doIt();
    }
}

class SubClass extends MainClass {
    @Override
    public doIt() {
        super.doIt();
        ...
    }
}

Now the problem is:

  • I call SubClass.doIt()
  • MainClass.doIt() is called
  • MainClass.doIt() makes recursion calling doIt()
    But: the SubClass.doIt() is called instead of MainClass.doIt()

That is very strange behaviour and problems are programmed! I tried to call the recursion with this.doIt() but that didn't help. Someone has an idea?

Thanks alot for your answers, this problem is solved.

That's the supposed behavior, by not setting a method final , that means it can be override n, so you must always take into account someone can do this. Call's to that method are never guaranteed to be to the method at that level.

You can however solve this problem elegantly, by using a ( protected ) final method:

class MainClass {

    protected final void innerDoIt () { //final: so no @Override
        ...
        else innerDoIt();
    }

    public void doIt() {
        innerDoIt();
    }

}

And then:

class SubClass extends MainClass {

    @Override
    public doIt() {
        super.doIt();
        ...
    }
}

final ensures, the method can't be overriden. So at that moment, you have a contract (guarantee) that the innerDoIt method is indeed the innerDoIt method you think it is.

So in case you don't wan't the caller to get overriden , simply hedge it into another final method. By making it protected , that method can also be called by the SubClass .

public class Main {

    public static void main(String[] args) {
        B b = new B();
        b.doIt();
        System.out.println();
        A a = new B();
        a.doIt();
    }


    public static class A{
        boolean check=false;
        public void doIt(){
            System.out.println("A start");
            if(check){

            }
            else{
                check = true;
                doIt();
            }
            System.out.println("A end");
        }
    }
    public static class B extends A{
        @Override
        public void doIt() {
            System.out.println("B start");
            super.doIt();
            System.out.println("B end");
        }
    }
}

In this example, both b and a are instances of class B , so as you probably expect, a.doIt() and b.doIt() will output the same result.

B start
A start
B start
A start
A end
B end
A end
B end

B start
A start
B start
A start
A end
B end
A end
B end

When you call doIt() , you are implicitly calling this.doIt() , and this is an instance of class B . There is no syntax to do what you want do without seperating the content of doIt() (see CommuSoft's answer)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM