0

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));
    }
5
  • You can do that if there is-a relationship between them Commented Nov 16, 2018 at 9:18
  • 1
    if EmailContact and PhoneContact extend Contact you can just add them to the ArrayList<Contact> list Commented Nov 16, 2018 at 9:18
  • con.add(new EmailContact(date, email)); instead Commented Nov 16, 2018 at 9:19
  • @TrungNTNguyen Thank you for this. Commented Nov 16, 2018 at 9:28
  • what do you want to do with the array content? as it will be awkward when retrieving objects because you will not really know if it's an EmailContact or PhoneContact, and more so you will not be able to use the getEmail respectively getPhone methods, only if you explicitly cast the contacts retrieved to their expected Class, which will often raise an ClassCastException Commented Nov 16, 2018 at 9:33

4 Answers 4

4

Wildcards are here to help you. One can add any class that extends Contact class to a list which accepts ? super Contact

List<? super Contact> list= new ArrayList<>();
list.add(new EmailContact());
list.add(new PhoneContact());
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for this. Do you know how can i get them separately in different methods?
Both methods need to share same list, take it as a parameter or declare it as instance variable
@nits.kk can you elaborate a bit please? I'm new to java and don't quite understand your logic.
2
import java.util.ArrayList;
import java.util.List;

public class Contact {


    public static void main(String[] args) {
        List<? super Contact>  list = new ArrayList<>();
        list.add(new PContact());
        list.add(new EContact());

    }
}

class PContact extends Contact{

}
class EContact extends Contact{

}

Comments

0

This is basic polymorphism in action. Reference can be of super type but it can refer to object of any sub type. The keyword new instantiates an Object. You need to provide the actual implementation with new. In your case

 Contact con = new PhoneContact(date, phone);
 Contact anotherCon = new EmailContact(date, email); 

So now with ArrayList you can do as below.

List<Contact> list = new ArrayList<>();
list.add(new EmailContact(date,email);
list.add(new PhoneContact(date,phone);

Comments

0

Assuming this is the hierarchy :

abstract class Contact { }

class EmailContact extends Contact { }

class PhoneContact extends Contact { }

you can do the following :

List<? super Contact> myList = new ArrayList<>();
myList.add(new EmailContact());
myList.add(new PhoneContact());

The wildcard signifies that you can add any class that extends Contact to the list.

Do note that when you fetch an element from myList, the only guarantee is that it will return a type of Object. So do not try to cast it without checking the right instance of Contact otherwise you will end up with a ClassCastException.

For eg:

Object contact = myList.get(0);        
if (contact instanceof EmailContact) {
    EmailContact emailContact = (EmailContact) contact;
} else if (contact instanceof PhoneContact) {
    PhoneContact phoneContact = (PhoneContact) contact;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.