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

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

Now I am looking at the interfaces and java implementation, and it looks like the same thing. First, you simply name the variables and methods in one class, and then define them in another class.

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

+6
source share
8 answers

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

C ++ has a text compilation model. So, in order to use something - for example, a function - in code, the compiler must first analyze the definition of that function. Putting things in header files that you want to use from many source files eliminates the need to rewrite the definition of a function, because you can include the same header file in many source files that use things in this header.

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

void PrintMessage(std::string text); 

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

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

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

Java works where function definitions are for you when compiling a project, when it views all the files in the project. C ++ only compiles one .cpp file at a time and looks only at #include header files.

The Java interface is equivalent to the C ++ base class. This is essentially a declaration of a set of methods, including the types of arguments they accept and the type of return values. The Java interface or the C ++ base class can be inherited by the Java class or the C ++ class, which actually defines (implements) these methods.

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

+20
source

Not. Java interface is an abstract C ++ class. Consider this:

 // Java interface Entity { void func(); } class EntityImpl implements Entity { public void func() { System.out.println("func()"); } } 

against.

 // C++ struct Entity { virtual void func() = 0; virtual ~Entity() {} // just to emphasize it a BASE class protected: Entity() {} // not required }; struct EntityImpl : Entity { void func() { std::cout << "func()" << std::endl; } }; 
+5
source

NO, an interface is a construct that even exists in C ++ as abstract classes. It has nothing to do with the section in the .hpp and .cpp files. The interface defines a certain set of functions that can then be overridden by child classes / implementation

+2
source

No, they are completely different.

Java does not have the equivalent of a β€œheader file”: separating the declaration of the API / data structure and implementing your classes simply does not exist. If you want to reference some third-party classes, you will need the .class files that you need when you want to run third-party classes (note that they are usually stored in .jar files, but this is just for convenience).

And the closest thing is that C ++ for Java interfaces is pure virtual classes: for example, classes that define a set of methods, but do not define any implementations for them. As in Java, you will need to create some subclass for them so that they are really useful, and these subclasses will provide an implementation.

+2
source

They are very different.

The Java interface is more like a pure abstract class in C ++, where none of the methods have implementations. They serve as a contract with the object to ensure that methods can be invoked.

The main reason interfaces exist is because Java does not have multiple inheritance , which means that a class can distribute only one superclass. However, a class can implement several interfaces, so you can define any number of operations that you can perform in a class through any number of interfaces.

+2
source

Not really.

First, the Java interface cannot contain variables, only constants and methods.

Secondly, Java interfaces should be created only where necessary, i.e. where you have an API that is implemented by several classes, and you want to be able to write code that processes them evenly. Most Java classes do not have to have an appropriate interface, and most Java interfaces need to have two or more implementation classes.

+2
source

There is no header file, it mainly contains code that we can use somewhere somewhere, where the interface is just a contract, how it should be implemented is not the same

0
source

C ++ is old, it is built on C, which is even older. In C ++, a class is really a C-style struct with a pointer to a vtable containing pointers to the starting address of each function in the class.

What you put in your header file is a class definition, i.e. struct. What you included in our implementation is the function that vtable points to. This is why you need to include the definition ("header") of the class in another file in order to be able to use it. That is why you usually put the class definition in a separate file (.hpp). You strictly do not need to, but it would be difficult to use this class in other classes if you do not. But you could put all your code in one large text file.

The C ++ compiler processes each file on its own, expanding all #include macros to include their definitions in each .cpp file. It also converts all function calls to vtable note, i.e.

someInstance-> someMethod () becomes (someInstance-> VTABLE [0]), assuming someMethod () is the first one declared in the definition of the someIntance class.

Then the compiler creates object files (the so-called translation units) and gives the resulting processing code for all functions implemented in all .cpp files in the memory layout, with movement tables for all pointers in all vtables. Later, the linker replaces the symbol characters in the movement table with the actual function memory address.

In short, C ++ looks like it is done for legacy purposes (and the fact that C is not object oriented). Java is object oriented from get go, which means that interfaces are what you CAN use, unlike C ++, where class definitions are what you SHOULD have (struct and vtable, remember) and what you MUST include, one way or another, in every .cpp where you want to use this class.

-1
source

Source: https://habr.com/ru/post/895237/


All Articles