0
import java.util.ArrayList;
import java.util.List;

public class Document {
    private List <DocumentManager> DocumentElements= new ArrayList<DocumentManager>();

    public void AddElement(DocumentManager d){
        this.DocumentElements.add(d);
    }

    public DocumentManager getWidest(){
        int top = 0;
        DocumentManager toReturn;

        for (DocumentManager d: DocumentElements){

            if (d.getWidth() > top){
                toReturn = d;
            }
        }
        return toReturn;

    }
}

We see that DocumentManager is the interface implemented, and in the Get Widest method, I'd like to return an Object that implements this interface (This can be a textbox, picture box, or a colorbox).

Upon trying to return a DocumentManager implementing Object (Here called toReturn), I encounter an error. Is there a way to do this?

ArifMustafa
  • 4,617
  • 5
  • 40
  • 48
  • did you try returning an implementation there instead? – Naman Feb 05 '18 at 04:43
  • You're not maintaining a reference to the currently widest `DocumentManager` in your loop, so `d.getWidth() > top` will most likely end up returning the last `DocumentManager` – MadProgrammer Feb 05 '18 at 04:45
  • You should post a small example of your entire code including your void main and the interface you are trying to implement along with the error message you are getting. This will help people better understand what you are trying to do. – Ghos3t Feb 05 '18 at 05:00

0 Answers0