What is the point of using abstract methods?

前端 未结 7 1475
春和景丽
春和景丽 2020-12-13 06:28

What\'s the point of using \"abstract methods\"? An abstract class cannot be instantiated, but what about the abstract methods? Are they just here to say \"you have to imple

7条回答
  •  天命终不由人
    2020-12-13 06:52

    If all an abstract does is declare abstract methods, you are correct, it's a bit silly, and an interface would likely be superior.

    But often an abstract class implements some (maybe even all) of the methods, leaving only a few as abstract. For example, AbstractTableModel. This saves rewriting a lot of code.

    Another "advantage" over an interface is that an abstract class can declare fields for subclasses to use. So, if you are pretty sure that any reasonable implementation would have a String named uniqueID, you could declare it, plus relevant getters/setters, in the abstract class, saving some typing later.

提交回复
热议问题