Why are interfaces preferred to abstract classes?

前端 未结 23 1433
鱼传尺愫
鱼传尺愫 2020-12-02 06:23

I recently attended an interview and they asked me the question \"Why Interfaces are preferred over Abstract classes?\"

I tried giving a few answers like:

23条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 07:06

    There is one reason not mentioned by the above.

    You can decorate any interface easily with java.lang.reflect.Proxy allowing you to add custom code at runtime to any method in the given interface. It is very powerful.

    See http://tutorials.jenkov.com/java-reflection/dynamic-proxies.html for a tutorial.

提交回复
热议问题