-1

So I'm trying to do a really simple interface and super class, and I'm getting an issue with my @Override statements saying annotation type not applicable to this kind of declaration. Now I saw this StackOverflow questions that said it was a simple spelling error, but I checked my interface and my class and the function signatures are the same. Here is the interface:

package cit260.harrypotter.view;
public interface ViewInterface {
    public void display();
    public String[]getInputs();
    public String getInput(String promptMessage);
    public boolean doAction(String[] inputs);
}

and here is the super class:

package cit260.harrypotter.view;
import java.util.Scanner;
public abstract class View implements ViewInterface {

    public View() {


        @Override
         public void display(){
            boolean endView = false;
            do {
                String[] inputs = this.getInputs(); 
                endView = doAction(inputs);
            } while (!endView);
        }

    @Override
        public String getInput(String promptMessage) {
            String input;
            boolean valid = false;
            while(!valid) {
                System.out.println(promptMessage);
                Scanner keyboard = new Scanner(System.in);
                input = keyboard.nextLine(); input.trim();
                if (input.length != 0){
                    System.out.println("Please enter a valid input");
                    valid = true;
                }
            }
            return input;
        }
    }
}

What am I doing wrong??

Adam McGurk
  • 186
  • 1
  • 19
  • 54
  • Your class won't compile. You have put all your methods in constructor View and not in class. What it means is close View(){} and then you are set. – Optional Nov 11 '17 at 03:41
  • @Optional thanks! mayooran barely beat you to the punch, but I moved it out and it all works! My question is now, how is input an integer? It is getting input from the keyboard, there is nothing integer specific to it – Adam McGurk Nov 11 '17 at 03:43
  • 1
    No issue with Mayooran. I am sorry, i didn't mean integer. It's not initialized. So return may say error at compile time that it has to be initialized. – Optional Nov 11 '17 at 03:45

2 Answers2

2

You have declared the methods inside the contructor. View() is a constructor which is used for initializing values for your class. You need to take your methods outside the constructor. Place them within the class alongside your constructor. Use an IDE to develop. Then these errors could be figured out while you are writing the code.

AnOldSoul
  • 4,017
  • 12
  • 57
  • 118
1

A constructor is used for initializing the value of the variable in your class. If you are implementing an interface then you have to override the method of the interface in your class.

P.K
  • 263
  • 2
  • 9