0

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?

DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83
  • 1
    Don't declare in subclass fields with same name as in superclass. This way you are creating separate fields but since you are giving them same name you are *shadowing* already existing fields. Anyway at `Log.d(TAG, "BaseNft: " + txHash); //Returns null` you are printing value of `txHash` declared in superclas, but in subclass you *overrode* setter to initialize *separate* `txHash` field (declared in subclass builder). So don't declare same fields if they ware already declared in superclass. And don't override setters for those fields. – Pshemo Jan 16 '22 at 08:56
  • 1
    code is not calling the methods from parent class, ergo the fields of parent class are not being set. (an (indirect) consequence of having two fields for same 8attribute* as already written in previous comment) A subclass already includes the fields of the parent class(es) no *need* to repeat these. – user16320675 Jan 16 '22 at 08:58
  • @Pshemo, user16320675 That fixed it. Honestly I wouldn't have figured it out by myself. Thank you. – DIRTY DAVE Jan 16 '22 at 09:03

0 Answers0