0% found this document useful (0 votes)
160 views2 pages

CSC 418 Lecture 3 Notes

This document compares the mechanisms for separating interface and implementation in C++ and Java. C++ allows stronger separation by declaring interfaces in header files and implementations in separate files with the .cc suffix. Java requires both interface and implementation to be together in a single .java file, so does not truly separate them like C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views2 pages

CSC 418 Lecture 3 Notes

This document compares the mechanisms for separating interface and implementation in C++ and Java. C++ allows stronger separation by declaring interfaces in header files and implementations in separate files with the .cc suffix. Java requires both interface and implementation to be together in a single .java file, so does not truly separate them like C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

LECTURE THREE (3): DATA ABSTRACTION; SEPARATION OF INTERFACE AND

IMPLEMENTATION

A key principle that is part of both the procedural and object-oriented paradigms is data

abstraction: the separation of the interface of a data type/class from its implementation.

However, the mechanisms for doing this in C++ support a stronger separation than those in Java

do.

C++

 C++ allows the interface of a class to be declared in a separate file from its

implementation.

 It is common practice to put the declaration of a class in a separate header file. (i.e, as an

aside -but an important point for people used to Java to note. The declaration of a C++

class always ends with a semicolon. Forgetting this can lead to really strange compiler

errors!)

 Client files will use a #include directive to include the header file. When a header file is

included, the effect is the same as if the text of the header file were physically present in

the source file, in place of the #include directive.

 The implementation goes in an implementation file whose name is typically the same as

that of the header, but with a “.cc” suffix. The implementation file also “#include”’s the

related header file.

o Note that a header (.h) file can (and often does) include declarations for

multiple related classes, in which case the implementation (.cc) file will

typically include implementations for each class as well.

2020/2021 LECTURE NOTES ON COMPARATIVE PROGRAMMING LANGUAGES (CSC 418) 1


 C++ does allow declaration and implementation to be done in one place (as in Java); but

this is not normally considered good practice. In particular, if the implementation code is

placed in the class declaration, then every time a source file includes the declaration, the

compiler will create a new copy of the implementation code.

Java

 Java does not really support the kind of separation of interface and implementation

that C++ does.

 Each publicly accessible Java class must be placed in its own source (.java) file. This

source file includes both the declaration and the full implementation of the class.

When a java source file is compiled, the resulting .class file contains a representation of

both the method declarations and their implementations.

 Client classes will import the class. This causes the compiler to extract the declaration

information from the compiled .class file.

 The java doc program does allow the interface portion of this file to be extracted to a

separate html documentation file; but this is only useful to a human reader, not client

code.

2020/2021 LECTURE NOTES ON COMPARATIVE PROGRAMMING LANGUAGES (CSC 418) 2

You might also like