0

I Know there are a bunch of questions related with this topic. Most of the answers states "use a interface" or "create a generic". Tried both and didnt work =( dont know what I'm doing wrong.

Here is the problem: I have two classes: Subjects and Courses. In the main section, I want a function that will receive an id (provided by the user) and look into, for example, a subjectList, trying to find if the subject is there, if yes , return its index. The logic is the same for both Courses and Subjects, so I'm trying to let this function become a bit more generic.

 public static void main(String[] args) {
        
             //Objects created along the code are stored here
             ArrayList<Course> courseList = new ArrayList<>();   
             ArrayList<Subject> subjectList = new ArrayList<>();
        
            public static Integer findObjectById(int id, ArrayList<IStudyDetails> object_list) {
                for (int i = 0; i < object_list.size(); i++) {
                    if (id == object_list.get(i).getId()) {
                        return i;
                    }
                }
                return null;
            }
    int index_subject = findObjectById(subject_id,subjectList);
    }

Here is the Interface: I tried to create this after look into some Stack Overflow related topics.

public interface IStudyDetails{
    int getId();

}

Here is the Course class: (I did hide most of the constructors/gets and setters)

public class Course implements IStudyDetails {
    private int id;
    private String name;
    public int getId() {return id;}}

Here is the Subject class (I did hide most of the constructors/gets and setters)

public class Subject implements IStudyDetails {
    private int id;
    private String name;
    public int getId() {return id;}}

THe error I'm receiving is:

java: incompatible types: java.util.ArrayList<entities.Subject> cannot be converted to java.util.ArrayList<entities.IStudyDetails>

4
  • 8
    Change it to ArrayList<? extends IStudyDetails>. Commented Jan 27, 2022 at 1:26
  • 2
    @shmosel can you make this an answer and explain ? Also ,is changing subjectList and courseList type to ArrayList<IStudyDetails> wrong ? Commented Jan 27, 2022 at 1:42
  • 1
    @YasserCHENIK See also What is PECS (Producer Extends Consumer Super)? Commented Jan 27, 2022 at 1:52
  • @shmosel wow, it worked! I agree with YasserChenik, could you explain a bit better in this specific context? Commented Jan 27, 2022 at 2:12

1 Answer 1

1
public static void main(String[] args) {
    //Objects created along the code are stored here
    List<IStudyDetails> courseList = new ArrayList<>();
    List<IStudyDetails> subjectList = new ArrayList<>();
    Subject subject = new Subject(0001, "John");
    Subject subject1 = new Subject(1001, "Tom");
    subjectList.add(subject);
    subjectList.add(subject1);
    int subject_id = 1001;
    int index_subject = findObjectById(subject_id,subjectList);
}
public static Integer findObjectById(int id, List<IStudyDetails> object_list) {
    for (int i = 0; i < object_list.size(); i++) {
        if (id == object_list.get(i).getId()) {
            System.out.println(i);
            return i;
        }
    }
    return null;
}

I Just change the List Type, Cause both Course and Subject are implemented from IStudyDetails, so you need to use IStudyDetails as a type to create the list and it can work.

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

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.