An interface in Java is a specific language construct or keyword. Java has an interface keyword that is used to indicate that the Java class does not provide the actual implementation, but instead describes the interface that the derived class should implement. This is checked by the Java compiler to ensure that the class that claims to implement the interface provides all the necessary methods for the interface.
In Java, the interface keyword indicates that the class provides a set of services described by the specified interface class. The Java class can implement several different interfaces.
The way the word interface is used in discussions with C ++ is somewhat similar, since it is related to the types of parameters of a class or function or method. However, in C ++, the interface keyword is missing. The word interface is used in a more general descriptive way with C ++, as in "The function interface takes two short and long." which indicates the function calling the arguments and their types, the interface between the calling function and the body of the function.
Therefore, think of the C ++ interface as some kind of contract between a class that implements the interface and any objects that use objects created from the class. A class can also actually provide several different related services, each of which has a specific interface.
In C ++, you can do something similar to the concept of a Java interface by using pure virtual methods in the class description to create an abstract class. This creates a C ++ class without implementation. See C ++: create an abstract class with an abstract method and override the method in a subclass .
What a virtual method does is provide a mechanism so that objects using derived classes of a class can depend on a particular interface, leaving implementation details for the derived class. So it looks like a Java interface. C ++ virtual methods are a way to provide a Java interface implementation using a compiled and static proven approach, rather than a run-time approach.
Using a virtual method, you create a type of superclass that can be used to create pointer variables to which variables from derived types can be assigned, and when you use the virtual method, the correct method from the derived class is called. Figuring out which invocation method is executed at compile time, rather than runtime. The basic idea for C ++ should be as effective as C, providing object-oriented language constructs along with static type checking at compile time to reduce the dependency on runtime error detection with additional overhead.
class Joe { public: virtual int thingOne() { return 1;}
With C ++, you need to recognize the difference between an object and a pointer to an object due to the splitting of objects that can occur when a variable containing a derived object is assigned to a variable containing the superclass of the class. This " object slice " will happen because the derived object does not fit into the base class or the superclass object from which it is derived. The derived object has additional material for which the superclass object does not have this purpose (the default assignment is a direct copy of the memory) only part The superclass of the derived object is copied to the superclass object.
class Joe { public: virtual int thingOne() { return 1;}