0

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;
1
  • 2
    If you have List<T> list where T is 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. Commented Mar 31, 2017 at 6:15

3 Answers 3

1

Here is what I can understand from the source.

List<ContactBO> contactList = new Gson().fromJson(jsonArray.toString(),
                new TypeToken<List<ContactBO>>() {}.getType());

This does not mean

As the code save List of object in list of Interfaces and can read it back

The above line converts response using GSON to a List object of type ContactBO. By implementing BusinessObject interface, ContactBO is still a class and not an interface.

Now the next line

List<BusinessObject> list = new ArrayList<BusinessObject>();

This again creates a List of type BusinessObject

Now the following line. list.addAll(contactList); Adds object of type ContactList to super object which is of type BusinessObject

Sign up to request clarification or add additional context in comments.

1 Comment

ContactBO is still a class and not an interface. Nice thumbs up and It means implementing interface (is a form of inheritance right)
0

"Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object."

   List<BusinessObject> list = new ArrayList<BusinessObject>();

This line basically show Polymorphsim concept, Parent class reference can hold the Child class object.

Comments

0

When you cast an object of a class into an interface, which that class implements you can only access implemented method(s) that the interface has.

3 Comments

But I am still getting attributes from it.
@Nepster It's important to understand the difference between "the type as Java sees it", and "the type that's actually in the object". If Int is an interface, and you have a variable Int x;, then Java knows that x has all the methods of Int, but it doesn't know anything else about x, so it can't let you access any other members with x.member. But x was created with some concrete class, and it will always be in that class. The other members will still be there, as long as Java knows that the object has that class...
@Nepster So if you say Int x = something; Concrete y = (Concrete)x; now Java knows that the object is Concrete (if it isn't, an exception will be thrown), so now you can access the members that were always in the object.

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.