I need to store different objects in the ArrayList. Objects are similar in nature but different and have different methods.
Circle c = new Circle();
Cube s =
This is what you have to do.
Define a Shape interface..
Implement the interface for Circle and Cube
Create shape objects for Circle and Cube and add these to arraylist..
Code below:
public interface Shape {
public void draw();
}
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Drawing Circle");
}
}
public class Cube implements Shape {
@Override
public void draw() {
System.out.println("Drawing Cube");
}
}
public class Simulator{
public static void main(String[] s){
Shape s1 = new Circle();
Shape s2 = new Cube();
ArrayList shapeList = new ArrayList();
shapeList.add(s1);
shapeList.add(s2);
}
}