abstract console input class and class deriving from it
Hi,
i have created a simple class that reads and writes from the console. As i may want to program other code in the future that has a console interface, i want to keep this class as generic as possible.
But when i implement it (extend it), i get an error like so when compiling:
DerivingClass.java:32: non-static method checkForInput() cannot be referenced from a static context
checkForInput();
I'm not sure that the way i'm trying to make the ConsoleInterface generic is a good anyway.
What would be a good way to achieve this and how can i solve the error?
The ConsoleInterface.java code:
import java.lang.*;
import java.util.*;
publicabstractclass ConsoleInterface{
BufferedReader console_in =null;
PrintWriter console_out =null;
String line =null;
publicvoid checkForInput(){
try{
console_in =new BufferedReader(new InputStreamReader(System.in) );
console_out =new PrintWriter(new OutputStreamWriter(System.out) );
while ( (line = console_in.readLine()) !=null ){
processInterfaceCommand();
}
printExitStatement();
console_in.close();
console_out.close();
}
catch (IOException e){
e.printStackTrace(console_out);
}
}
publicabstractvoid processInterfaceCommand();
publicabstractvoid printExitStatement();
}
The DerivingClass.java code
import java.net.*;
import java.io.*;
publicclass DerivingClassextends ConsoleInterface{
public DerivingClass(){
System.out.println("Starting DerivingClass ");
}
publicvoid processInterfaceCommand(){
// repeat incoming text
console_out.println("You wrote " + line);
console_out.flush();
// ending?
if ( line.equalsIgnoreCase("BYE") ){
return;
}
elseif ( line.equalsIgnoreCase("HELLO") ){
console_out.println("Hello back at ya ");
console_out.flush();
}
}
publicvoid printExitStatement(){
console_out.println("Have a nice day :)");
console_out.flush();
}
publicstaticvoid main(String[] args)throws IOException{
DerivingClass serv =new DerivingClass();
// do other stuff before going in check loop
// ...
checkForInput();
}
}

