Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it correct that header files in C++ and abstract class/interface implementations in Java are both the same idea?

I am a little with familiar with C++ and I know that for almost every header file, I must create source file to go with it.

Now I am looking into java interfaces and implementation and it looks as the same thing. At first you just name the variables and methods in one class and then you define them in other class.

Are these things in C++ and Java basically the same or similar?

like image 410
Jaanus Avatar asked Aug 17 '11 11:08

Jaanus


People also ask

Are header files the same as interfaces?

Fundamentally, headers and interface s 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.

Can we use interface and abstract class together?

The instance of an abstract class can't be created. Now as all methods in an interface are abstract methods therefore we can implement it using Abstract Class.

Are there header files in Java?

javah generates C header and source files that are needed to implement native methods. The generated header and source files are used by C programs to reference an object's instance variables from native source code. The . h file contains a struct definition whose layout parallels the layout of the corresponding class.

Which of the following is not a true statement about interfaces and abstract classes?

Answer and Explanation: 1. The only true statement is C), All methods defined in an interface must be implemented when used by another class. Option A) is not correct because an interface can have a number of user defined methods.


1 Answers

Java interfaces and C++ header/implementation files are different concepts.

C++ has a text compilation model. So, to use something - such as a function - in your code, the compiler must first have parsed a definition of that function. By putting things into header files that you want to use from many source files, it saves you having to re-write out the definition of a function because you can include the same header file into many source files that use the things in that header.

Functions in C++ can be declared by just writing the function name and arguments:

void PrintMessage(std::string text);

And they can be defined by writing the method body too:

void PrintMessage(std::string text)
{
  cout << text;
}  

You can only define a function once in a compilation unit (which is all the text the compiler sees after the #includes have been replaced with the text of the file they include). But, you can declare a function many times as long as the declarations are the same. You must define every function that gets called once. That's why you have a .cpp file for each .h file. The .cpp defines all of the functions that are declared in the .h file. The .h file gets included into all of the the .cpp files that use the functions and gets included once into the .cpp file that defines the functions.

Java works out where the definitions of functions are for you when it compiles a project as it looks at all of the files in the project. C++ only compiles one .cpp file at a time and only looks at #included header files.

A Java interface is equivalent to a C++ abstract base class. It's essentially a declaration of a set of methods including the types of arguments they take and the type of their return values. A Java interface or C++ abstract base class can be inherited by a Java class or C++ class which actually defines (implements) what those methods are.

In C++, when you create a class, you usually (there are exceptions) put the method declarations into a header file, and you put the definitions in the .cpp file. But, in Java, you only need to write definitions of methods, these definitions do the equivalent of C++ definition and declaration in one. You can put all the java method definitions in one file.

like image 112
Scott Langham Avatar answered Sep 20 '22 11:09

Scott Langham