0

I have my subclass:

public class Actions extends Main{

  public void getFireTarget() {
      GameObject target = getGameObjects().closest("Target");
      do{
          log("Shooting at the target");
          getMouse().click(target);
      } while(target != null && !getDialogues().inDialogue() && getLocalPlayer().getTile().equals(rangeTile));
  }
}

I want to write similar methods, so I can call them in my Main class, so I don't have to write over and over.

My main class looks like this (won't fully paste it as it's long):

public class Main extends AbstractScript{
...code here
Actions actions = new Actions();
}

So I am trying to implement the methods in Actions by doing actions.getFireTarget(), which seems to work. But when I compile, I am getting two compile errors: 1) In the Main class, in the line: Actions actions = new Actions(); 2) In the Actions class, in the line where I am extending the superclass.

Am I missing something in the sub class in order to store methods and then call them in the main method? Please advise! Thanks

3
  • 2
    If I understand what you're asking correctly, you're wanting to implement a superclass that depends upon functionality in its subclasses. This doesn't make any sense. Commented Apr 17, 2015 at 2:09
  • I don't understand what you're saying. Please be more clear, possibly give a better code example Commented Apr 17, 2015 at 2:10
  • What is the compile error you are getting? I guarantee that if you show us that, everyone reading it (perhaps including yourself) will have a better idea of what your problem is. Commented Apr 17, 2015 at 2:13

3 Answers 3

1

The syntax is wrong. () are not allowed here: public class SomeName(). Remove the brackets.

Sign up to request clarification or add additional context in comments.

1 Comment

Like @Paul said, you are combining constructor and class definition syntaxes.
0

I am having trouble understanding your details. Here is a short info about inheritance:

public class A{
  protected int t;
  public void methodA(){}
}

public class B extends A{
  @Override
  public void methodA(){}
  public void methodB(){}
}

If you override the methodA in class B, any call from a instance of class B to the method will use the method defined in class B. (If you dont write the method in class B, it will use the method from class A)

Objects of class A cannot use methodB() defined in class B. Also you can access the field t in class B, because of the protected modified.

2 Comments

Can you give me the compiler error. You are trying to user subclass objects in the superclass? can you give us a bit more code or details?
Trying to use the subclass methods in the superclass. These are the two classes: pastie.org/10096783 pastie.org/10096784
0

I think its better for you your problem to instantiate other class to your main class if you have same function name or same variable name. try to compile and run this code

public class First{
    Second sec = new Second();
    String s = "This is first";
    public First(){

        System.out.println(this.s);
        System.out.println(getSecondString());
        System.out.println(sec.getSecondString());
    }
    public String getSecondString(){
        return "This is first";
    }
    public static void main(String args[]){
        new First();
    }
}

public class Second {

    String s = "This is second";
    public String getSecondString(){
        return s;
    }

}

1 Comment

its possible that you are creating a loop by calling the function of Actions class from the Main class and calling the function of the Main class from Action class. it will cause this exception: "Exception in thread "main" java.lang.StackOverflowError"... try to check your logic in your program. Maybe you have logic error....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.