Java Inheritance – Enforcing Implementation of Superclass Public Methods

inheritancejava

I have the following :

public Class A {

    public void methodA() {
        ....
    }

    public void methodB() {
        ....
    }

}

I don't have control over Class A. I would like to create subclasses of Class A with a restriction that it must override methodA() (super.methodA() call must run the implementation of Class A). is it possible? is so, how?

Best Answer

First thing to understand here is that this part of requirements: super.methodA() means this applies to the code of the classes extending A, in a way defined in JLS 15.12.4.4-2. Method Invocation Using super

An overridden instance method of a superclass may be accessed by using the keyword super to access the members of the immediate superclass, bypassing any overriding declaration in the class that contains the method invocation...

Let's say, we want a way to create some specific subclasses of A such that whenever super.methodA() is invoked in these, we are guaranteed that instead of code that was in class A (out of our control) it will run some other code, defined in some subclass of A (under our control).


Let's see what "tools" are at our disposal to achieve above.

  1. Need to create some, specific subclasses of A brings to mind traditional way to specialize - subclassing, that is we'll consider subclass of A (let's call it B) that would redefine (override) methodA in the way we want.

  2. Need to force a subclass to provide a code that would run where we want it in superclass can be satisfied by defining an abstract method, let's call it forcedImplementationInSubclass()


Now we've got all what we need.

  • We will define a class B extending A, so that all subclasses of B will also be subclasses of A.
  • In B, we will override methodA, so that its implementation in class A won't be available to subclasses of B anymore.
  • In overridden method in B we will invoke abstract method forcedImplementationInSubclass() which will guarantee that subclasses of B will have to define the code that will eventually run in methodA.

As a result, any subclass of B won't be able to invoke methodA implementation provided in class A and instead, will be guaranteed to run code defined in some subclass of B.

Extending B is the way to create specific subclasses of A that behave as we want. The code could look about as follows:

public abstract class B extends A {
    // override, so that subclasses of B won't be able to get to super.methodA
    @Override public void methodA() {
        // invoke abstract method here (subclass would have to implement it)
        forcedImplementationInSubclass();
    }

    // define abstract method so that subclass would have to implement it
    protected abstract void forcedImplementationInSubclass();
}

// extending B is the way to get what we need
public class C extends B {
    // enforced implementation of the abstract method
    @Override protected void forcedImplementationInSubclass() {
        // that will be invoked when B.methodA() is called, and since B is-a A...
        System.out.print("Hello from implemented methodA");
    }

    public void testSuperMethodA() {
        // that will invoke B.methodA() which won't go to class A
        super.methodA();
    }

    public static void main(String[] args) {
        A a = new C();
        a.methodA(); // goes to B.methodA(), which in turn invokes C.forcedImplementationInSubclass()
        C c = new C();
        c.testSuperMethodA(); // won't go to class A
    }
}