0

I have abstract class Tester, with abstract method test() and main method:

    abstract class Tester {

     abstract String test();

     public static void main(String[] args) {
          doSomething();

          Tester t = new Tester() {
          @Override
              String test() {
                return "";
              }
          };        
          result = t.test();

          doSomethingElse();
     }            
    }

Obviously t.test() will always return "" for this class and any other subclasses.

But I want to call method test() inside of main method, that way that any subclass will have its own test method called at its execution (in its inherited main method).

Is that even possible in way I imagined ? Can I somehow declare object t as a subclass object (without knowing his name) so he can use his own implementation of this abstract method ?

N10
  • 349
  • 2
  • 3
  • 15

1 Answers1

0

Seems to me that you haven't thought through exactly how you wish to design your application / object structure. Try seeing if there is a better way you can organise your objects.

The only way you could make your situation work is if you had a test() method that delegated to the current instance's test0() method or similar. That's a trick Java uses internally in some places.

md_5
  • 632
  • 9
  • 26