Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force subclass to call an abstract implemented method

Basically, what I want to do is force the subclass to call an abstract superclass method (implemented in the subclass), so I don't have to explicitly write it each time I create a new subclass.

I wrote it in the superclass' constructor once, because I want it to force it for every implementation.

public abstract class SupahClass {
    public SupahClass() {
        doStuff(); // It IS executed when the subclass constructor is called
        init(); // NOT executed, even though it's implemented
    }

    private void doStuff() { ... }        

    protected abstract void init();
}

public class SomeSubClass extends SupahClass {

    // The problem lies HERE: this is executed AFTER init() ... so it gets NULL again
    private TextBox myTextBox = null;

    public SomeSubClass() {
        super(); // invokes the super constructor, so init() should be called
        // I could call init(); here EACH time i create a new subclass... but no :)
    }

    @Override
    public void init() {
        this.myTextBox = new TextBox(); // Executed BEFORE its declared as null above
    }
}

Of course, the superclass can't really call it since its an abstract (so undefined) method, but its an ABSTRACT class so it cannot be instanciated, it must delegate the task to its subclasses, so why cant they call the abstract but now implemented method?

EDIT See the subclass property myTextBox and the init() implementation

Which approach do you think I should do? Remove the = null in the property declaration (duhhh)

or remove the init() in the superclass and explicitly calling it in the subclass constructor (this is what I wanted to avoid, since I will have to write it 100% of the time..)

like image 719
dominicbri7 Avatar asked Jul 20 '11 13:07

dominicbri7


2 Answers

I can't reproduce this. init() will be called, assuming no exceptions are thrown first. Short but complete example:

abstract class Superclass {
   public Superclass() {
       init();
   }

   protected abstract void init();
}

class Subclass extends Superclass {
   public Subclass() {
       super();
   }

   @Override
   public void init() {
       System.out.println("Subclass.init called");
   }
}

public class Test {    
    public static void main(String[] args) throws Exception {
        new Subclass();
    }
}

That prints "Subclass.init called" as expected. I suspect something else is wrong in code that you haven't shown us.

Note that calling virtual methods in a constructor is a risky business - the subclass won't have been initialized yet - all the variables will have their default values, for example. It's generally a pattern to be avoided.

like image 135
Jon Skeet Avatar answered Sep 20 '22 01:09

Jon Skeet


You can fix this by changing your declaration to:

private TextBox myTextBox;

The assignment to null serves no useful purpose. If there was no superclass, it would do nothing, because fields are initialised to null anyway. Since there is a superclass, it acts as a gunshot to the foot. So, get rid of it.

like image 20
Tom Anderson Avatar answered Sep 21 '22 01:09

Tom Anderson