Abstract class properties

1. Abstract classes are not allowed to be instantiated, only inherited. Abstract classes can contain properties and methods. Methods may or may not contain code implementations. Methods that do not contain code implementations are called abstract methods. 3. Subclasses inherit from abstract classes and must implement all abstract methods in the abstract class.

Interface features

Interfaces must not contain attributes (that is, member variables). Interfaces must declare methods, and methods must not contain code implementations.

The role of abstract classes

1, code reuse 2, polymorphic elegant implementation of the second point specific explanation: inheritance does not necessarily achieve polymorphism, inheritance + rewrite to achieve polymorphism. Polymorphism does not necessarily require abstract classes, but abstract classes can improve maintainability and readability. If a method is an empty implementation rather than an abstract class, subclasses may forget to override the method. Also, an empty implementation can make maintainers feel strange.

Functions of interfaces

Decoupling. An interface is an abstraction of behavior, equivalent to a set of protocols. The caller only needs to focus on the abstract interface without understanding the concrete implementation, reducing the coupling between the codes.

C++ mimics interfaces through abstract classes

C++ has only abstract classes and no interfaces. There are no member variables. There are only method declarations and no method implementations. The class that implements the interface must implement all methods in the interface

class Strategy {	// Use abstract classes to simulate interfaces
	public:
		~Strategy(a);virtual void algorithm(a) = 0;
	protected:
		Strategy(a); };Copy the code

The abstract class Strategy does not define any attributes, and all methods are declared as virtual (equivalent to the Java abstract keyword). Then all methods cannot be implemented in code, and every subclass that inherits the abstract class must implement the method. Syntactically, this abstract class is equivalent to an interface

Use ordinary classes to implement interfaces

Change the constructor from public to protected, throw an exception in the method implementation, and force the subclass to implement the methods in the parent class. Next, use ordinary classes in Java to simulate interfaces

public class MockInteface {
	protected MockInteface(a) {}
	public void funcA(a) {
		throw newMethodUnSupportedException(); }}Copy the code

MethodUnSupportedException abnormalities, to simulate the does not include the implementation of the interface, and the ability to force a subclass in the inheritance of the parent class when all active implementation of the parent class method, otherwise it will be an exception at run time.

The difference between

The important differences between interfaces and abstractions, beyond syntactic features, and most importantly conceptually, are that abstractions are more like a kind of definition, whereas interfaces are more like protocols. The former is the relationship of belonging and inheritance, while the interface is the relationship of inclusion and possession. Abstract: IS-a Interface :has-a: indicates that it has certain functions. From the perspective of class inheritance level, abstract class is a bottom-up design idea. The code of existing subclasses is repeated and then abstracted into the upper parent class. Interface, on the other hand, is a top-down design idea, generally design interface first, then to consider the concrete implementation.