I'm following the implementation of subclassing a builder class from this stackoverflow answer.
https://stackoverflow.com/a/17165079/11110509
I've been staring at my code for an hour now comparing it to the one in that answer. I can't for the life of me figure out what I'm doing incorrectly because the builder
passed from my child class to my parent
class is giving me null values.
My parent class:
public class BaseNft {
private static final String TAG = "BaseNft";
private final String itemName;
private final String itemDescription;
private final String ownerOf;
private final String txHash;
protected BaseNft(Builder<?> builder) {
itemName = builder.itemName;
itemDescription = builder.itemDescription;
ownerOf = builder.ownerOf;
txHash = builder.txHash;
Log.d(TAG, "BaseNft: " + txHash); //Returns null
}
public static class Builder<T extends Builder<T>> {
private String itemName;
private String itemDescription;
private String ownerOf;
private String txHash;
public T itemName(String itemName) {
this.itemName = itemName;
return (T) this;
}
public T itemDescription(String itemDescription) {
this.itemDescription = itemDescription;
return (T) this;
}
public T ownerOf(String ownerOf) {
this.ownerOf = ownerOf;
return (T) this;
}
public T txHash(String txHash) {
this.txHash = txHash;
return (T) this;
}
public BaseNft build() {
return new BaseNft(this);
}
}
}
My child class:
public class Card extends BaseNft {
protected Card(Builder builder) {
super(builder);
}
public static class Builder extends BaseNft.Builder<Builder> {
private String cardName;
private String cardDescription;
private String ownerOf;
private String txHash;
public Builder itemName(String itemName) {
this.cardName = itemName;
return this;
}
public Builder itemDescription(String itemDescription) {
this.cardDescription = itemDescription;
return this;
}
public Builder ownerOf(String ownerOf) {
this.ownerOf = ownerOf;
return this;
}
public Builder txHash(String txHash) {
this.txHash = txHash;
return this;
}
public Card build() {
return new Card(this);
}
}
}
Card card = new Card.Builder()
.itemName("name")
.itemDescription("description")
.ownerOf("owner name")
.txHash("test txhash")
.build();
I've traced the card creation with logs and in the build()
method for my child class
public Card build() {
//successfully returns values
return new Card(this);
}
it successfully returns the name, description, owner, and txhash.
But in the parent class's constructor BaseNft
they return null. I can't figure out why. Could someone please help me figure out what I'm doing incorrectly?