Inheritance and Generics with Abstract Data Types

In this article, I want to demonstrate a simple inheritance example that uses generics.  The abstract parent class is generic in the type, and it defines an abstract method that accepts as a method parameter the generic data type.

The generic data type used by the extending child in the implementation of the abstract method. This example is going to discuss the benefit of this approach.

Consider the following parent class:

public abstract class Parent<T> {
    protected abstract void process(T data);
}

Consider the following two child classes that extend the parent:

public class Child extends Parent<SomeClass> {
    @Override
    public void process(SomeClass data) {
        // Do stuff
    }
}
public class AnotherChild extends Parent<OtherClass> {
    @Override
    public void process(OtherClass data) {
        // Do stuff
    }
}

You can clearly see how this approach creates flexibility when multiple child classes require different data type parameters when implementing the abstract method. Because of the generic type setting in the abstract method, any data type can be passed to the child. This approach can be particularly useful when implementing a strategy design pattern.