How and when to use an abstract class

前端 未结 6 1608
梦毁少年i
梦毁少年i 2020-12-04 12:42

This is my test program in Java. I want to know how much abstract class is more important here and why we use abstract class for this.

Is it a mandatory or is it bes

6条回答
  •  天涯浪人
    2020-12-04 13:30

    An abstract class has an "is-a" type relationship with your subclasses. So for instance, you could have an abstract class Shape which has stuff any shape has (like a draw function), and then a class SquareShape. Every squareshape is a shape, but not all shapes are squareshapes.

    In you example you have 2 shape-types, and a class that has 2 instances of these. That is not a relationship you should define with abstract I think.

    You might want to do something with your names though, because this is a rather small example, it's hard to see the real implications of the files, and how they should work.

提交回复
热议问题