Frank
Frank

Reputation: 19

Access Array of objects from different class

So here is the problem. I have one array, there are two classes of object in it. Email which is the superclass and UniversityEmail which is the inheritance class. The array is of type Email. I wanna print out all the object of type Email from the array. Or print out UniversityEmail object based on a private variable extends by UniversityEmail class. Any idea?

Upvotes: 0

Views: 89

Answers (1)

Obl Tobl
Obl Tobl

Reputation: 5684

Try the following:

for (int i = 0; i < array.length; i++) {
  if (array[i] instanceof Email) && (!(array[i] instanceof UniversityEmail)) {
    //print your object 
  }  
}

That's typed just from mind, so there might be some smaller issues.

Upvotes: 1

Related Questions