java– virtual functions, abstract functions, abstract classes, interfaces
1. Java virtual functions
The existence of virtual functions is for polymorphism.
C + + Ordinary member function plus virtual keyword becomes virtual function
In Java, there is no concept of virtual function, its ordinary function is equivalent to C + + virtual functions, dynamic binding is the default behavior of Java. If you do not want a function to have virtual function properties in Java, you can add the final keyword to a non-virtual function
PS: In fact, C + + and Java in the virtual function of the same point of view, similar.
2. Java abstract function ( pure virtual function )
An abstract function, or a pure virtual function, exists to define an interface.
The pure virtual function in C + + is as follows: virtual void print () = 0;
The pure virtual function in Java is: abstract void print ();
PS: In the abstract function of C + + and Java or the same.
3. Java abstract class
Abstract classes exist because the parent class includes both a specific definition of the subclass's generic function and a function interface that requires subclasses to implement them. The abstract class can have data members and Non-abstract methods.
Abstract classes in C + + only need to include pure virtual functions as an abstract class. If you include only virtual functions, you cannot define them as abstract classes, because there is actually no abstract concept in the class.
A Java abstract class is a class declared with an abstract decoration.
PS: Abstract class is actually a half virtual and half real things, can be all virtual, this time into an interface.
4. Java interface
Interfaces exist for the purpose of forming a statute. You cannot have ordinary member variables in an interface, and you cannot have a non-virtual virtual function.
The interface in C + + is actually a completely virtual base class.
In Java, an interface is a class decorated with interface.
PS: Interfaces are abstract classes that are virtual to the extreme.
5. Summary
C + + virtual function = = Java normal function
C + + pure virtual function = = Java abstract function
C + + abstract class = = Java abstract class
C + + Virtual base class = = Java interface