Java – Difference between header files and interfaces

cinterfacesjava

I wanted to know whether the header files in c and c++ have same function as that of interfaces used in Java? If not what is the difference between header file and interface?

Best Answer

Not quite. Definitions in a header file correspond more closely to public members of a Java class. Both header files and public/private access modifiers allow us to make a distinction between the publicly visible interface/API of our code, and its internal implementation details.

C or C++ declarations are subject to the One Definition Rule. A compiled program can contain only one implementation for each declared function or global variable (unless it is declared as inline, in which all definitions MUST be equivalent). So for one function declaration in a header, I write one function definition in a .c or .cpp file. If I want to use a different definition, I need to recompile the code.

Java interfaces (or C++ classes with pure virtual methods) also declare a kind of “interface”, but it is very different: one class can implement many interfaces, and one interface can be implemented by many classes. The latter point is very important: I can write code that only calls methods on an object through an interface. Later, I can provide objects that conform to this interface to that code, and the code will still work! I can choose multiple implementations at run time, without having to recompile the code. This provides a lot of flexibility, e.g. for dependency injection.

Fundamentally, headers and interfaces work very differently. Headers contain forward declarations that are resolved later, but still during compilation. An interface (or non-final class) defines virtual methods that are resolved at run time through dynamic dispatch.

They also mean different concepts. Mechanisms like headers and public/private access modifiers in classes allow us to implement abstract data types (ADTs). ADTs encapsulate implementation details of some data structure, and only allow external code to access to those structures through public functions or methods. In contrast, interfaces and virtual methods allow us to implement objects. One interface can be implemented by many different objects. This ability of objects of one common interface to behave differently is also called polymorphism.

ADTs and objects both have something to do with “encapsulation”, but they do that very differently. They should be understood as complementary concepts.

However, there has been significant interest in object-oriented solutions. E.g. the Design Patterns book investigates how applying object-oriented techniques like dynamic dispatch can introduce enough flexibility into a software system to solve some interesting problems. As a random example, the Composite Pattern allows us to treat collections of items equivalently to a single item. How? both the collection and single items implement the same interface. This wouldn't be possible just with header files.