简体   繁体   中英

Abstract method in the class constructor

I want to implement an Abstract Java class. One of the abstract methods must be implemented by each child class, to ensure that that part of the code will be executed individually in each child class. Is there any other way to do it and thus avoid the "warning" that appears by calling an abstract method from the constructor of the main class?

    public abstract class Listener {

    protected Date checkTime = new Date();
    protected TypeUpdate type = UNKNOW;

    public Listener(){
        super();
        this.setTypeListener();
    }

    public void setTime(Date date) {
        if (date != null) {
            return;
        }
        this.checkTime = date;
    }

    /* Abstract methods */
    public abstract void execute();

    protected abstract void setTypeListener();
    }

Thank you. ------------------------------ EDITED ----------

Ok, It's an error call an abstract method in constructor. So, what can I do to force inheriting classes to make a concrete constructor implementation (for example, initialize a member in one way or another?)

I want to implement an Abstract Java class. One of the abstract methods must be implemented by each child class, to ensure that that part of the code will be executed individually in each child class. Is there any other way to do it and thus avoid the "warning" that appears by calling an abstract method from the constructor of the main class?

    public abstract class Listener {

    protected Date checkTime = new Date();
    protected TypeUpdate type = UNKNOW;

    public Listener(){
        super();
        this.setTypeListener();
    }

    public void setTime(Date date) {
        if (date != null) {
            return;
        }
        this.checkTime = date;
    }

    /* Abstract methods */
    public abstract void execute();

    protected abstract void setTypeListener();
    }

Thank you. ------------------------------ EDITED ----------

Ok, It's an error call an abstract method in constructor. So, what can I do to force inheriting classes to make a concrete constructor implementation (for example, initialize a member in one way or another?)

You are arriving in the base class constructor and on returning from that constructor, in the child class constructor all field initialisations will happen and the remaining code form the child constructor.

To ensure that some method is called you can either call that method lazily later. Or pass an independent object, as "part" of the entire child.

public Child() {
    super(createPart());
}

private static Part createPart() {
    return new Part().withAnswer(42);
}

public Base(Part part) { ... }

One may also have a case of

  • service offered (public final)
  • requirement implemented (abstract protected)

So:

class Base {

     public final void foo() {
         onFoo();
     }

     protected void onFoo() {
         throw new IllegalStateException("Missing onFoo implementation "
             + getClass().getName());
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM