Question

I've got an abstract class called customer and another classe called payCust that extends this abstract class. There is another client class that initializes a list of customers.List<Customer> custList = new ArrayList<>();

the customer class has a constructor as follows:

public Customer(String name, String email, String mag) {
        this.CusEmail = email;
        this.CusName = name;
        this.magazine = mag;
    }

payCust has the following constructor:

public PayCust(String _name, String _email, String _accountType, String _custMag) {
        super(_name, _email, _custMag);
        this.accountType = _accountType;
    }

all the variables have public get and set methods. e.g.

public void setName(String name) {
            this.CusName = name;
        }
public String getName() {
        return this.CusName;
    }

my question is that if the custList had a PayCust added to it. how can i edit the accountType of a customer from that list? note: email is unique to every customer

Was it helpful?

Solution

You will have to check the instance type of the object within the ArrayList and cast it for usage.

Something like this, for example:

for (Customer c : custList){
    if(c instanceof PayCust){
        PayCust pc = (PayCust) c;
        pc.getAccountType();
    }
}

OTHER TIPS

You would have to cast it to a PayCust (assuming you know for a fact that it's a PayCust):

PayCust example = (PayCust) custList.get(0);
String accountType = example.getAccountType ();
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top