bsoolius
bsoolius

Reputation: 75

Extending super class attribute

I have a class Wallet, which extends to 3 different wallets PaypalWallet, BankWallet and ChequeWallet. Each with different attributes save the super class attributes.

These wallets are used in Payout, which extends 2 payments, PaymentBank and PayoutCheque.

public class Payout {
    @Id private String id;
    private String userId;
    protected Wallet userWalletInfo;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public Wallet getUserWalletInfo() {
        return userWalletInfo;
    }

    public void setUserWalletInfo(Wallet userWalletInfo) {
        this.userWalletInfo = userWalletInfo;
    }
}

If I inherit this class in the ChequePayout, how can I specify that userWalletInfo should be an instance of ChequeWallet? Similar to extending the class?

My first thought was to override the setter, like this:

public class PayoutCheque extends Payout {
    private String serialNumber;
    private ChequeStatus status;

    public String getSerialNumber() {
        return serialNumber;
    }

    public void setSerialNumber(String serialNumber) {
        this.serialNumber = serialNumber;
    }

    public ChequeStatus getStatus() {
        return status;
    }

    public void setStatus(ChequeStatus status) {
        this.status = status;
    }

    @Override
    public void setUserWalletInfo(Wallet userWalletInfo) {
        if (userWalletInfo instanceof ChequeWallet) {
            super.setUserWalletInfo(userWalletInfo);
        }
        else {
            //throw exception
        }
    }
}

However I don't feel like this is the right approach, or that I am missing something important here.

Upvotes: 1

Views: 1905

Answers (2)

Dishonered
Dishonered

Reputation: 8851

if(userWalletInfo.getClass().equals(ChequeWallet.class)){

}

Upvotes: 0

OldCurmudgeon
OldCurmudgeon

Reputation: 65851

Sounds like you are looking for generics.

private class Wallet {
}

private class ChequeStatus {
}

private class ChequeWallet extends Wallet {
}

public class Payout<W extends Wallet> {
    private String id;
    private String userId;
    protected W userWalletInfo;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public Wallet getUserWalletInfo() {
        return userWalletInfo;
    }

    public void setUserWalletInfo(W userWalletInfo) {
        this.userWalletInfo = userWalletInfo;
    }
}

public class PayoutCheque extends Payout<ChequeWallet> {
    private String serialNumber;
    private ChequeStatus status;

    public String getSerialNumber() {
        return serialNumber;
    }

    public void setSerialNumber(String serialNumber) {
        this.serialNumber = serialNumber;
    }

    public ChequeStatus getStatus() {
        return status;
    }

    public void setStatus(ChequeStatus status) {
        this.status = status;
    }

    @Override
    public void setUserWalletInfo(ChequeWallet userWalletInfo) {
        super.setUserWalletInfo(userWalletInfo);
    }
}

Upvotes: 2

Related Questions