Java and C # are easy to use, but they are never understood in details. For example, I just found that abstract is also available in Java. For example, C # has the readonly field and has the const function.
From: http://hi.baidu.com/tsj2209/blog/item/99dd931a59d879dfad6e759f.html
Abstract class and interface support the definition of abstract classes in Java. Abstract class and interface have great similarity in support for abstract class definition, and can even be replaced with each other. In fact, there is a big difference between the two. Their choices even reflect the understanding of the nature of the problem domain, and whether the understanding of the design intent is correct and reasonable.
You cannot create an abstract class instance. However, you can create a variable whose type is an abstract class and point it to an instance of a specific subclass. Abstract constructors or abstract static methods are not allowed. The subclasses of abstract classes provide implementation for all abstract methods in their parent classes. Otherwise, they are also abstract classes.
An interface is a variant of an abstract class. All methods in the interface are abstract. Multi-inheritance can be achieved by implementing such an interface. None of the methods in the interface has a program body. The interface can only define static final member variables. The implementation of an interface is similar to that of a subclass, except that the implementation class cannot inherit behaviors from the interface definition. When a class implements a special interface, it defines (to be given by the program body) all the methods of this interface. Then, it can call the interface method on any object that implements the interface class. Because there is an abstract class, it allows the interface name as the type of the referenced variable. Normally, dynamic Association editing will take effect. The reference can be converted to the interface type or from the interface type. The instanceof operator can be used to determine whether the class of an object implements the interface.
Interfaces can inherit interfaces. Abstract classes can be implemented (implements) interfaces. abstract classes can inherit object classes, provided that the object classes must have clear constructors. The interface is more concerned with "what functions can be implemented", regardless of "how to implement ".
1. Similarities
A. Both are abstract classes and cannot be instantiated.
B. The interface implementation class and the subclass of the objective CT class must implement the declared abstract method.
2. Differences
A. The interface must be implemented and implements must be used. abstract class must inherit and extends must be used.
B. A class can implement multiple interfaces, but a class can inherit only one abstract class.
C. Interface emphasizes the implementation of specific functions, while abstract class emphasizes the ownership relationship.
D. Although the interface implementation class and the class subclass must implement the corresponding abstract method, the implementation form is different. Every method in the interface is an abstract method and is declared only (Declaration, no method body). The implementation class must be implemented. Abstract class sub-classes can be selectively implemented.
This option has two meanings:
First, not all methods in the abastract class are abstract. Only those methods with abstract information are abstract. sub-classes must be implemented. Methods without abstract must be defined in objective CT class.
Second, when the subclass of abstract class inherits from it, non-abstract methods can be directly inherited or overwritten. For abstract methods, you can choose to implement them, you can also declare its method as an abstract method, without implementation, and leave it to its subclass for implementation. However, this class must also be declared as an abstract class. It is an abstract class and cannot be instantiated.
E. abstract class is the intermediary between interface and class.
Interfaces are completely abstract and can only declare methods. They can only declare pulic methods, private and protected methods, method bodies, and instance variables. However, the interface can declare constant variables, and it is not difficult to find such an example in JDK. However, putting constant variables in an interface violates its purpose as an interface and obfuscated the different values of interfaces and classes. If necessary, you can put it in the corresponding abstract class or class.
Abstract class plays an important role in interface and class. Abstract class is abstract and can declare abstract methods to standardize the functions that must be implemented by subclass. On the other hand, abstract class can define the default method body for subclass to use or override directly. In addition, it can also define its own instance variables for subclass to use through inheritance.
3. Application scenarios of interfaces
A. the class and the class need to coordinate with specific interfaces, regardless of how they are implemented.
B. It can exist as an identifier that can implement specific functions, or it can be a pure identifier that does not exist in any interface method.
C. Consider a group of classes as a single