Hi all, I have question. Say I have a super class called Super and two subclasses called Sub1 and Sub2. I also have two ArrayLists for each type of the subclasses. Now I'm wondering if I can have a single method that puts the argument of type Super into the correct list, like this:
Is there a way to get around having to cast item to the correct subtype as item is guaranteed to be of the correct type if it passes the if statement. Is this a safe way to perform this task?
you have a super class called Person
you have two subclasses called Driver and Teacher (both extends Person)
Now, Driver and Teacher cannot be cast into one anoteher, because a Driver IS-NOT a Teacher and Teacher IS-NOT a Driever but both are Persons.
You can have a method whcih accepts the list of Person and can add both Driver and Teacher to Person class. But you can not have any single method that can take a single list and add appropriately in the Driver and Teacher without casting.