0

I am trying to understand how inhertitance and ArrayList work, so I have this following code It has a class which is the database where data will be saved

public class Database {
    ArrayList<Student> students;
    ArrayList<Course> courses;

    public boolean doesIDExist(ArrayList<RegistrationSystem> array, int id){
       boolean exist = false;
       for (RegistrationSystem array1 : array) {
           if (array1.getId() == id) {
              exist = true;
              break;
           }
       }
       return exist;
    }

    public boolean addStudent(int id, String name, String surname){
       if( doesIDExist(students, id)){
           return false;
       }

       students.add(new Student(id, name, surname));
       return true;
    }

}

Both Student and Course are subclasses of Registration System

public class RegistrationSystem  {
   protected int id;

   public int getId() {
      return id;
   }
}

But I get an error in this line:

       if( doesIDExist(students, id))

incompatible types: ArrayList< Student > cannot be converted to ArrayList< RegistrationSystem >

I cannot quite understand why I get this error!

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
hodor92
  • 15
  • 4
  • 2
    You probably want to make that `RegistrationSystem` superclass an interface instead... Saying Student "is a" RegistrationSystem or Course "is a" RegistrationSystem makes no sense, so having an inheritance relationship between them probably makes no sense either. – Jean-François Corbett Apr 30 '15 at 10:37

5 Answers5

5

Your method expected a List of RegistrationSystem not a List of Student.

You have to change to:

public boolean doesIDExist(ArrayList<? extends RegistrationSystem> array, int id){
Jens
  • 67,715
  • 15
  • 98
  • 113
  • Maybe it's worth to explain the upper bounded wildcards a bit more in detail and not only present the solution here. see [Oracle tutorial - upper bounded wildcards](https://docs.oracle.com/javase/tutorial/java/generics/upperBounded.html) – SubOptimal Apr 30 '15 at 10:36
  • 1
    to expand on @Jens answer, neither `ArrayList` or `ArrayList` are subtypes of `ArrayList`; you must use the wildcard syntax `? extends RegistrationSystem` in this scenario to make your methods flexible enough to accept a collection of any subtype of `RegistrationSystem` – Tom Bunting Apr 30 '15 at 10:37
1

In here if( doesIDExist(students, id), You are passing Student objects instead of RegistrationSystem objects. As a better option you have to implement a new method to identify the existing student id's

Sanshayan
  • 1,076
  • 1
  • 11
  • 16
1

Although Student is a subtype of RegistrationSystem, it is not the case that ArrayList<Student> is a subtype of ArrayList<RegistrationSystem>.

The reason for this, is that this could result in non-type-safe code. Consider the following code:

void method1(ArrayList<RegistrationSystem> listOfRegistrationSystems) {
    listOfRegistrationSystems.add(new Course()); 
}
void method2() {
    ArrayList<Student> listOfStudents = new ArrayList<>();
    method1(listOfStudents);
    Student student = listOfStudents.get(0);
    // Ooops! The first element of listOfStudents is not a Student!
}

To solve this, Java provides bounded type variables and bounded wildcards. You can use the ArrayList<? extends RegistrationSystem>, which means: an ArrayList of instances of some unknown subclass of RegistrationSystem. In your case, ArrayList<? extends RegistrationSystem> can be one of ArrayList<RegistrationSystem>, ArrayList<Student> or ArrayList<Course>. So you have to change your method to

public boolean doesIDExist(ArrayList<? extends RegistrationSystem> array, int id){
    boolean exist = false;
    for (RegistrationSystem array1 : array) {
        if (array1.getId() == id) {
            exist = true;
            break;
        }
    }
    return exist;
}
Hoopje
  • 12,677
  • 8
  • 34
  • 50
  • Excellent explanation. To add to this, its worth noting that: a) the generic type itself _can_ be substituted in calling statements for one of its subtypes (e.g. a method that takes `List extends Number>` could accept `ArrayList`) and b) when using the wildcard syntax as discussed you will only be able to get instances of the upper bounded type (e.g. `RegistrationSystem` out of the collection and most importantly you cannot put anything _into_ the collection - see [PECS](http://stackoverflow.com/questions/2723397/java-generics-what-is-pecs) – Tom Bunting Apr 30 '15 at 11:20
0

What your trying to do here is something like this:

ArrayList<RegistrationSystem> array = students;

Java does not allow this

Change your method signature do this instead doesIDExist(ArrayList<? extends RegistrationSystem>,int id)

Nikhil Talreja
  • 2,754
  • 1
  • 14
  • 20
0

You are trying to pass an ArrayList of Student objects, when the method accepts RegistrationSystem only.

Vasco Lameiras
  • 344
  • 3
  • 15