Design Patterns Java - Template method Php

1) What have we done?

We just created class "ClassB". We changed there step3.

public class ClassA
{
    public void doSomething(boolean doStep2)
    {
        System.out.println("doing step one");
        if (doStep2)
        {
            System.out.println("doing step two");
        }
        System.out.println("doing step three");
    }
}

public class ClassB extends ClassA
{
    @Override
    public void doSomething(boolean doStep2)
    {
        System.out.println("doing step one");
        if (doStep2)
        {
            System.out.println("doing step two");
        }
        System.out.println("doing changed step three");
    }
}

public class Client
{
    public static void main(String[] args)
    {
        ClassA classA = new ClassB();
        classA.doSomething(true);
    }
}



2) Why is this wrong?

We we can see we have duplication of code in "ClassA" and "ClassB"

The whole algorithm is unchanged there is only changed step 3.

3) How we can improve this?

We can apply "Template Method" pattern means implement template method responsible for algorithm and implementation of particular steps move to subclasses.