I have seen someone's code and unable to understand how it works.
As the code save List of object in list of Interfaces and can read it back.
Please explain how it works. And can we save Object in interface (where Object implements it). AFAIK interface doesn't have members. How this code save and retrieve please explain.
ContactBO implementing BusinessObject
public class ContactBO implements BusinessObject {
private String id, fullName, department, ;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public boolean isSection() {
// TODO Auto-generated method stub
return false;
}
}
Where BussinessObject
public interface BusinessObject {
boolean isSection();
}
Saving list of Contact code
List<ContactBO> contactList = new Gson().fromJson(jsonArray.toString(),
new TypeToken<List<ContactBO>>() {}.getType());
List<BusinessObject> list = new ArrayList<BusinessObject>();
list.addAll(contactList);
read List of ContactBO using List of Interface
List<ContactBO> contactListItems = (List<ContactBO>) (List<?>) result.list;//where list is public List<BusinessObject> list;
List<T> listwhereTis an interface, the objects of the list can be objects of any classes that implement the interface. The objects themselves don't change when you put the in the list. They still have the same class they have when they're created. Please try reading a tutorial such as this one.