I have 3 classes. Contact, EmailContact, and PhoneContact. I want to create an Arraylist that can contain objects from both EmailContact and PhoneContact. And i need to find a way to get those objects. Here is what i have so far but It doesn't seem to split them like i wanted to.
public void addEmailContact(String date, String email) {
ArrayList<Contact> con = new ArrayList<>();
con.add(new Contact(date, email));
}
public void addPhoneContact(String date, String phone) {
ArrayList<Contact> con = new ArrayList<>();
con.add(new Contact(date, phone));
}
is-arelationship between themEmailContactandPhoneContactextendContactyou can just add them to theArrayList<Contact>listgetEmailrespectivelygetPhonemethods, only if you explicitly cast the contacts retrieved to their expected Class, which will often raise anClassCastException