Interface versus concrete class

前端 未结 5 704
北荒
北荒 2021-02-05 12:06

Below I have a Person interface, an implementing class and a driver class which initialises the Person with a name and just outputs it again. What is the advantage of using

5条回答
  •  一向
    一向 (楼主)
    2021-02-05 12:55

    Using the interface instead of the concrete class would let you change the implementation later.

    This about JDBC, all of it is based on interfaces, so the driver can implement it later.

    For instance when you use a ResulSet you don't really care how or what underlaying implementation is ( with a Oracle Driver would be something like OracleResultSet, with a MySQL driver could be something like MySQLResultSet ) but you know what methods are available for you to use.

    Same goes with List or Map, instead of ArrayList or HashMap

提交回复
热议问题