Java Abstract Class Confusion: no overridden method

So, I have two classes. One 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 of them is a specific 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, there is a call to the abstract displayOptions () method.

I have an overridden displayOptions () in a subclass, so I assumed that it would call the subclass method, but it didn't seem to do that. Is there a way to do this, or did I make a clear mistake or did I misunderstand how abstract classes should work?

PS I tried putting the print statement inside a subclass of displayOptions () to make sure I didn't do anything stupid with the method I call.

Many thanks,

Adam

+3
4

, - BasicClientOptions.displayOptions(). , , BasicClient.displayOptions() .

, . . , .

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");
    }
}
+4

? , .

: (, : -)

public/protected, . ()/, . , () .

(), , // () .

Edit:

, , , (, , @Override).

100% , ? System.out.println run , .

100% , - ( - , , , )?

+2

, , - ( ).

/ , . .

, , , , , .:)

.

import java.util.Scanner;

public abstract class AbstractClient {
  protected boolean running = true;

  protected void run() {
    Scanner scanner = new Scanner( "foo\\r\\nbar\\r\\n" );
    displayOptions();
    while ( running ) {
      String input = null;
      while ( scanner.hasNext() ) {
        input = scanner.next();
      }
      processInputCommand( input );
      running = false;
    }
  }

  abstract void displayOptions();

  abstract void processInputCommand( String input );

}

import java.rmi.RMISecurityManager;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

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();
    System.out.println( "We're in subclasses displayOptions()." );
  }

  @Override
  void processInputCommand( String input ) {
    System.out.println( "We're in subclasses processInputCommand()." );
  }
}

We're in subclasses displayOptions().
We're in subclasses processInputCommand().

, , , , .

, .

0

, , , , , - .

, . 10 , 2 8 . , .

, NetBeans , VM , , (, ) . , Java, , (-, , , ).

After wearing my laptop for several hours, I found that adding the abstract stub of the method that I was calling to the abstract class allowed me to redirect the call to a specific class without any complaints from the virtual machine. Is this normal or am I just lucky?

In any case, I hope someone finds this useful.

0
source

Source: https://habr.com/ru/post/1704779/


All Articles