c++ pure-abstract-class == java interface


In C++ we generally separate a class into two modules: a .h module and a .cc module. The .h module contains the definition of the class, and the .cc module contains the definition of that class’s member functions. The definition of a class, in the .h module, contains declarations of all the member functions and member variables of the class.

This information goes beyond simple interface.

All the utility functions and private variables needed by the class are also declared in the .h module. These utilities and private variables
are part of the implementation of the class, yet they appear in the module that all users of the class must depend upon (via #include). Thus, in C++, implementation is not automatically separated from pure interface.

This lack of separation between pure interface and implementation in C++ can be dealt with by using purely abstract classes. A purely abstract class is a class that contains nothing (no fields) but pure virtual functions (ending in “=0”). Note Forward class declaration in effC++ can have fields, so those classes are usually not PAC.

c++ purely-abstract-class (PAC) == Java interface. SUN saw the value of PAC and made it a first class citizen in java.
c++ class definition + method definition == java class definition
c++ class with at least one pure virtual method == java abstract class

c++ forward class declaration == java no such thing
c++ regular class header file (without method definition) == java no such thing

In terms of compile-time dependency, java favors dependency-on-interface. In c++, you usually depend on *.h files, which is “thicker” than java interfaces. I feel there are 2 ways to reduce this tight coupling

* Forward class declaration

In java, as a “client” class, we can be completely oblivion of the actual type responding to OUR message. That actual type could be unknown at compile time. No recompile required when the actual type changes, since we depend on the interface only. I feel this is the dream of c++ developers.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s