Mellon
Mellon

Reputation: 38882

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 ??

Upvotes: 1

Views: 89

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280141

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

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

Upvotes: 3

Related Questions