So what I want to do is extend my class 'TheTemplate' from my 'Squarer' class and override my abstract methods which will change the method 'isEnd' from 12 to 20. How do I do this?

Code can be seen below:

public class RunTemplateExample {

    public static void main(String[] args) {
        TheTemplate ttOne = new TimesTable((int) (Math.random() * 12 + 1)),
                ttTwo = new RandomGuesser(),ttTwo2 = new RandomGuesser();

        ttOne.templateMethod();
        ttTwo.templateMethod(); 
    }
}

abstract class TheTemplate {


    public void templateMethod() {
        doStart();
        while (!isEnd()) {
            doSomething();
        }
        doEnd();
    }

    abstract protected void doStart();

    abstract protected boolean isEnd();

    abstract protected void doSomething();

    abstract protected void doEnd();

}






public class TimesTable extends TheTemplate {

    private int count;
    private int table;

    public TimesTable(int table) {
        this.table = table;
    }

    protected void doStart() {
        System.out.println(table + " times table");
    }

    protected boolean isEnd() {
        return (++count > 12);
    }

    protected void doSomething() {
        System.out.println(count + " times " + table + " = " + (count * table));

    }

    protected void doEnd() {
        System.out.println("*******");
    }
}




class Squarer extends TheTemplate {



}

by writing the code like that? but why not just extend TimesTable and override the doEnd method, if that is the only method that is supposed to do something different?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.