Q:

Java arraylist of different object class

i have an issue with the understanding the arraylist which contain the objects from different classes. I have 6 objects. All of objects have 2 attributes in common.(ID,Type)
Every object has its own attributes. I created mainObject class with 2 atr (ID,Type). The other objects extends the mainObject so they have 
class mainClass{
    this.id=id;
    this.type=type;
}
class extendedClass extends mainClass{
    super(ID,Type);
    this.atr1=atr1;
}

class extendedClass2 extends mainClass{
    super(ID,type);
    this.atr2=atr2;
    this.atr3=atr3;
}

I read the informations from file.
FileInputStream fstream = new FileInputStream("data.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
    // Print the content on the console
    String s[] = strLine.split("\\|");
    mainClass myObj = new mainClass(s[0], s[1]);

ArrayList<mainClass> items = new ArrayList<mainClass>();
items.add(myObj);

I need all objects to be readed from file line by line and store them in the array list.
How should i do this? I tried the ArrayList<Object> list = new ArrayList<Object>, but its not working. The point is read all the objects from file, sort them due to chosen attribute(id,type).

A:

You are correct that you need a list of mainClass:
ArrayList<mainClass> items = new ArrayList<mainClass>();

However you should put this line before the while loop, not inside it.
ArrayList<mainClass> items = new ArrayList<mainClass>();

while ((strLine = br.readLine()) != null) {
    // etc...
    items.add(myObj);
}

