How to use polymorphism to make list with different types in java?

后端 未结 5 1303
北荒
北荒 2020-12-22 04:30

I have 3 Classes Circle, Rectangle and Square

I want to get required data for each of above classes and create them by user .

It me

5条回答
  •  悲哀的现实
    2020-12-22 05:23

    you can use the concept of inheritance to do this. for example, create a Shape class and then make the other classes inherit:

    public abstract class Shape {
      public int calculateArea();
    }
    public class Circle extends Shape {}
    public class Square extends Shape {}
    public class Rectangle extends Shape {}
    

    Then, you can use list of Shape.

提交回复
热议问题