how do i add different class objects to arraylist?

Yes: Use the abstract type:

List<Student> allStudents = new ArrayList<>();

One of the fundamentals of OOP is the Liskov substitution principle, which is basically that all types may safely be identified and dealt with using their abstract type.

Indeed, using the type List<Student> (as I have) instead of ArrayList<Student> is another example, and best practice.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top