How do i create an arraylist of many DIFFERENT OBJECTS and add their references. So like shape.add(new Rectangle(3, 2)); and like shape.add(new RightTriangle(2, 4).
1 Answer
If you can't make an interface for these classes, you can use wildcard
ArrayList<?> shapes = new ArrayList<>();
shapes.add(new WhateverYouWant());
2 Comments
Mike
So with that I can put anything I want meaning different objects like the shapes for example and it not throw an error at me
Henry Twist
Yes but there's not much point in this approach, when you retrieve them you won't be able to call any relevant methods etc. So they might as well not be in an array in the first place.
Shapeclass/interface. Then you can use an array list of shapes, not objects.Shape?)