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?
2
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.
-
Need to create some, specific subclasses of
A
brings to mind traditional way to specialize – subclassing, that is we’ll consider subclass ofA
(let’s call itB
) that would redefine (override)methodA
in the way we want. -
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
extendingA
, so that all subclasses ofB
will also be subclasses ofA
. - In
B
, we will overridemethodA
, so that its implementation in classA
won’t be available to subclasses ofB
anymore. - In overridden method in
B
we will invoke abstract methodforcedImplementationInSubclass()
which will guarantee that subclasses ofB
will have to define the code that will eventually run inmethodA
.
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
}
}
2
Once an method has been given an implementation, there is no way of making it abstract again. So, no, without changing A
there is no way of writing a class B extends A
so that all subclasses of B
must provide another implementation, at least not via the type system.
The best you can do is write a method in B
that throws an Exception at run time. This forces any subclass to override the method to be useful, but it will still compile without doing that.
3
I think you are talking about ‘abstract class’. If you want that the subclass of Class A must override the methodA() of Class A, then you have to declare the methodA() as abstract method and Class A is an abstract class.
If any class method having some implementation then it will act as a default implementation of the subclasses. Subclasses can optionally override that with their own implementation. If you want to give implementation to the subclass only and force the subclasses to follow a contract (method signature) then this is a clear situation of abstract class.
class A
{
void abstract methodA(); // No implementation.
}
class B extends A
{
void methodA()
{
// Subclass implementation.
}
}
3
Before using reflection API think about the cost of it.
It is simply easy to do. For instance:
C
subclass of B
and B
subclass of A
. All three have a method methodName()
for example.
public abstract class A {
public void methodName() {
System.out.println("Class A");
}
}
public class B extends A {
public void methodName() {
super.methodName();
System.out.println("Class B");
}
// Will call the super methodName
public void hackSuper() {
super.methodName();
}
}
public class C extends B {
public static void main(String[] args) {
A a = new C();
a.methodName();
}
@Override
public void methodName() {
/*super.methodName();*/
hackSuper();
System.out.println("Class C");
}
}
Run class C Output will be:
Class A
Class C
Instead of output:
Class A
Class B
Class C
1