Discuss the similarities and differences between java interface and C++ virtual class

  • 2020-05-19 04:53:52
  • OfStack

The C++ virtual class is equivalent to the abstract class in java. It differs from the interface in this way:

1.1 subclasses can only inherit one abstract class (virtual class), but can implement multiple interfaces

2.1 abstract classes can have constructors, and interfaces have no constructors

Methods in the 3.1 abstract classes are not abstract methods, that is, methods can be implemented (have method body), methods in the interface are abstract methods, can not have method body, only declaration

4.1 the abstract classes can be public, private, protected, default, and the interface is public only

The methods in the 5.1 abstract classes can be public, private, protected, default, and the methods in the interfaces can only be public and default

The same is true: neither can be instantiated.

Note: the interface is a special class of abstract class, is a more abstract abstract class, you may understand. An abstract class is an incomplete class, and the interface only defines some functionality. For example, the abstract class and the interface are used to describe "dog" respectively. The abstract class is to add abstrict before the first class to say: "pig can run on four legs, and pig can do this or that..." , the interface can only say: "pigs can run, with what run is a subclass to talk about"


Related articles: