Suppose you're creating a function to return an instance of the current type. Is there a way to make the type variable T refer to the precise subtype (so T should refer to B in class B)?
class A { <T extends A> void foo(); } class B extends A { @Override T foo(); }
To build on StriplingWarrior's response, the following design is required (a formula for a hierarchical fluent builder API):
Firstly, an abstract base class (or interface) that establishes the contract for retrieving the runtime type of an instance extending the class:
/** * @param <SELF> The runtime type of the implementer. */ abstract class SelfTyped<SELF extends SelfTyped<SELF>> { /** * @return This instance. */ abstract SELF self(); }
Intermediate extending classes must be abstract and maintain the recursive type parameter SELF:
public abstract class MyBaseClass<SELF extends MyBaseClass<SELF>> extends SelfTyped<SELF> { MyBaseClass() { } public SELF baseMethod() { //logic return self(); } }
Classes derived further can follow the same pattern. However, none of these classes can be utilized directly as types of variables without using raw types or wildcards (which undermines the purpose of the pattern). For instance (if MyClass wasn't abstract):
//wrong: raw type warning MyBaseClass mbc = new MyBaseClass().baseMethod(); //wrong: type argument is not within the bounds of SELF MyBaseClass<MyBaseClass> mbc2 = new MyBaseClass<MyBaseClass>().baseMethod(); //wrong: no way to correctly declare the type, as its parameter is recursive! MyBaseClass<MyBaseClass<MyBaseClass>> mbc3 = new MyBaseClass<MyBaseClass<MyBaseClass>>().baseMethod();
This is why these classes are referred to as "intermediate," and why they should all be declared abstract. "Leaf" classes are required to complete the loop and make use of the pattern, which resolve the inherited type parameter SELF with its type and implement self(). To prevent breaking the contract, they should also be marked as final:
public final class MyLeafClass extends MyBaseClass<MyLeafClass> { @Override MyLeafClass self() { return this; } public MyLeafClass leafMethod() { //logic return self(); //could also just return this } }
Using such classes makes the pattern usable:
MyLeafClass mlc = new MyLeafClass().baseMethod().leafMethod(); AnotherLeafClass alc = new AnotherLeafClass().baseMethod().anotherLeafMethod();
The primary advantage of this is that method calls can be chained up and down the class hierarchy while maintaining the same specific return type.
The above is the detailed content of How Can I Refer to the Current Type Using a Type Variable in Java?. For more information, please follow other related articles on the PHP Chinese website!