0

I'm working on a Java EE project, but there are methods which return a List of interfaces. Don't know the purpose for this or if it is a bad practice. Tried to look for list of interfaces but all the information appears like interface List.

    public List<SomeInterfaceRO> getAttribute() {
        return (List<SomeInterfaceRO>)someInterfaceBO
    }
public interface SomeInterfaceRO {

    public String getName();

    public String getComment();

// More methods ...  
}

The someInterfaceBO is a Java class which implements that interface.

EdUrbinaDev
  • 3
  • 1
  • 6
  • 1
    Possible duplicate of ["Program to an interface". What does it mean?](https://stackoverflow.com/questions/1992384/program-to-an-interface-what-does-it-mean) – luk2302 Sep 19 '19 at 13:46

1 Answers1

0

Consider this: when a method has on its signature that returns an Interface (eg. Cloneable) the rest of the code hears that that method return a class that extends that interface, not the interface itself.

A list of interfaces is just a list of objects of classes that implement that interface. Each one can be a different class, must all of them must implement the interface, because the rest of the code wants to assume that it can call some methods of those elements that are available on the interface.

For example, a List<Cloneable> can have as elements a Descriptor, a AclEntry, a CharacterIterator, a CertSelector. I personally don't even know what they are, but I can assume I can clone them, because they are Cloneable!

Hope I helped!

Sterconium
  • 559
  • 4
  • 20