samedi 12 novembre 2016

calling abstract method in the constructor

I read it somewhere that abstract methods should not be used in constructor. As part of my project, I have already called abstract method in the constructor .For example, Below is my AbstractController

public abstract class AbstractViewController<T> {

    public AbstractViewController(T t) {
        setValues(t);
        performAction(t);       
    }

    public abstract void setValues(T t);

    public abstract void performAction(T t);

}

public class EmployeeViewController extends AbstractViewController<NewEmployeeView>{

    public EmployeeViewController(NewEmployeeView t) {
        super(t);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void setValues(NewEmployeeView t) {
        // TODO Auto-generated method stub

    }

    @Override
    public void performAction(NewEmployeeView t) {
        // TODO Auto-generated method stub

    }

}

My intention was developer should not bother about calling setValues() and performAction() methods in the concrete class constructor. That was the reason I added these methods in the AsbtractClass constructor. Is this approach wrong? If it is wrong, what is the another option to implement this?

Aucun commentaire:

Enregistrer un commentaire