sideways8
sideways8

Reputation: 153

subclasses of an abstract class contain an object which can't access an implementation of an abstract method

Ok, I'll put the code first hopefully to make it clearer.

***Edit: Problem is solved, by passing the instance of the sequence to the dialog when the sequence gets created, then the dialog has the internal reference to call.

public abstract class RavelSequence {
    protected Dialog dialog; //every subclass uses one of these objects
    public abstract void hit();
}

public class BattleSequence extends RavelSequence {
    public void init(){                //this is the edited fix
        dialog.setSequence(this);      //
    }                                  //

    public void hit(){ //the effect of a 'hit' in battle
        doSomething();
    } 
}

public class OutOfBattleSequence extends RavelSequence {
    public void init(){                //this is the edited fix
        dialog.setSequence(this);      //
    }                                  //

    public void hit(){ //the effect of a 'hit' outside battle
        doSomethingElse();
    }
}

public class Dialog extends Container implements Runnable {
    private RavelSequence sequence;                  //this is the edited fix
    public void run (){
        if (somethingHappens)
            sequence.hit();
    }
    public void setSequence (RavelSeqence sequence){ //this is the edited fix
        this.sequence = sequence;                    //
    }                                                //
}

What I want to happen is for the Dialog to be able to call the method hit() implemented in whichever class owns the instance of Dialog. I am using IntelliJ IDEA and it tells me that the 'non-static method hit cannot be referenced from a static context.'
The whole thing is run inside an application which creates instances of the Sequence objects depending on the context of the game, so hit will need to reference non-static objects within the Sequence.

Upvotes: 2

Views: 1328

Answers (3)

Alexis Dufrenoy
Alexis Dufrenoy

Reputation: 11946

You're Dialog object can't know which RavelSequence it's part of. So what you're trying to do is not possible that way. Include a RavelSequence in your Dialog and it will work fine.

for example:

public class Dialog extends Container implements Runnable {
    protected RavelSequence sequence;

    public Dialog(RavelSequence sequence) {
        this.sequence = sequence; // or any other mean to set your RavelSequence: setter, dependency injection...
    }

    public void run (){
        if (somethingHappens)
            sequence.hit();
    }
}

Upvotes: 2

Garrett Hall
Garrett Hall

Reputation: 30022

Because hit() is not a static method you need to access the enclosing instance of RavelSequence in order to call it.

You do that by using the this keyword if Dialog is a nested class:

public abstract class RavelSequence {
    public class Dialog extends Container implements Runnable {
        public void run (){
            RavelSequence.this.hit();
        }
    }
    public abstract void hit();
}

Otherwise you will need to pass an instance of RavelSequence into the Dialog instance.

Upvotes: 2

jocelyn
jocelyn

Reputation: 898

You need an instance of a implementation of RavelSequence. Because RavelSequence is abstract it cannot be instanciated directly.

public class Dialog extends Container implements Runnable {
    public void run (){

        if (somethingHappens)
            RavelSequence  ravel = new OutOfBattleSequence(); // we choose implementation
            ravel.hit();
    }
}

Upvotes: 0

Related Questions