简体   繁体   中英

Invoke function in parent abstract class from child class in my case

I have a abstract class:

public abstract class Parent{

    public void cook(){
        DoSomething(); //call abstract method
    }

    protected abstract void DoSomething();

}

I have a concrete class which imiplements the above abstract class:

public Child extends Parent{
   private Toy toy;

   public void initToy(){
      toy.setOnPlayListener(new OnPlayListener() {
            @Override
            public void onPlay() {
                //How to call parent class cook() method here?
            }
        });
   }

   @Override
   public void DoSomething(){...}
}

I want to call the cook() method of Parent class under current Child instance in the override onPlay() function of OnPlayListener() in Child class. How to do it?

========Update=======

Thanks for your answers, now I would like to make it clear, are Child.super.cook() & Child.this.cook() the same thing ??

You need to get the enclosing class instance's super class

@Override
public void onPlay() {
    Child.super.cook();
}

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