Is programming against interfaces in Java the same concept as using header files in C/C++?

前端 未结 7 1361
失恋的感觉
失恋的感觉 2021-02-07 08:02

The java code I\'m working on at the moment has often a structure like

file Controller.java:

interface Controller {...}

file Controller

7条回答
  •  迷失自我
    2021-02-07 08:59

    interfaces are more like abstract base classes in c++.

    the reason they are often used in java is that multiple inheritance does not exist in java (both were design decisions). c++ supports multiple inheritance, so... it can accomplish the problem that way (and via a few others).

    once a class implements an interface in java, then it can be passed as the type. the same principle in c++, although the written implementation is slightly different.

    so, the interface simplifies the programming process, since arbitrary objects may be passed, as long as they implement the interface (or in c++ are derived from a specific class). with an interface, you do not need to derive from a common base class -- it's a very simple, and usable design. multiple inheritance in c++ is a pitfall for many developers.

提交回复
热议问题