So I have two classes. One is abstract:
public abstract class AbstractClient {
protected boolean running = true;
protected void run() {
Scanner scanner = new Scanner(System.in);
displayOptions();
while (running) {
String input = null;
while (scanner.hasNext()) {
input = scanner.next();
}
processInputCommand(input);
}
}
abstract void displayOptions();
abstract void processInputCommand(String input);
}
One is the concrete subclass:
public class BasicClient extends AbstractClient {
private IBasicServer basicServer;
public static void main(String[] args) {
new BasicClient();
}
public BasicClient() {
try {
System.setSecurityManager(new RMISecurityManager());
Registry registry = LocateRegistry.getRegistry();
basicServer = (IBasicServer) registry.lookup(IBasicServer.LOOKUPNAME);
run();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
void displayOptions() {
BasicClientOptions.displayOptions();
}
@Override
void processInputCommand(String input) {
// TODO Auto-generated method stub
}
}
Now in the subclass I call the run() method of the abstract class because this should be common to all clients. Inside the run() method is a call to the abstract method displayOptions().
I have overridden displayOptions() in the subclass so I assumed that it would invoke the subclassed method but it seems it has not. Is there a way to do this or have I made an obvious mistake or have I misunderstood how abstract classes should work?
P.S I tried putting a print statement inside the subclassed displayOptions() to ensure I hadn't done something daft with method I call.
Many thanks,
Adam
Maybe something is wrong with your BasicClientOptions.displayOptions() call. I'm wondering how you know that BasicClient.displayOptions() isn't being called.
Here's a simplified version of what you have. Try running it. It behaves in the way you expect.
public abstract class BaseClass {
public void run() { foo(); }
public abstract void foo();
}
public class Subclass extends BaseClass {
public static void main(String[] args) { new Subclass().run(); }
@Override
public void foo() {
System.out.println("I'm from the subclass");
}
}
Is it the case that the classes are in different packages? If so you need to declare the method to override as protected.
Edit: (guess an explanation might help :-)
If you declare a method public/protected then it can be overridden by children outside of the package. If you make it (package)/private then it cannot. private methods cannot be overridden at all which (package) ones can only be overridden by classes in the same package.
I use (package) because there is no keyword for it, so in the absence of public/protected/private you get (package) access.
Edit:
The above is likely not true given your description (assuming that the class really is abstract, and you have used the @Override annotation).
Are you 100% sure that the run method is getting called? Put a System.out.println in run and make sure it is called.
Are you 100% sure that you are not catching any other exceptions and failing to print out the stack trace (or something else that will ensure that you see that the exception was caught)?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With