I.Differences between classes and interfaces
Class:Describes an object, including its status and possible actions.
Interface:Defines the actions that an object may send. However, it only defines the prototype of these actions without implementation and any status information.
1.Differences between interfaces and Classes:
(1)An interface is like a standard and a protocol. It is an abstract concept;
(2)The class implements this protocol and satisfies the specific entity of this specification. It is a specific concept.
(3)SlaveProgramIn a simple way, the interface is the function declaration, and the class is the function implementation. Note that the same declaration may have many implementations.
2.Differences between interfaces and abstract classes:
(1) A class can implement any number of interfaces but can subclass at most one abstract class.
(2) An abstract class can have nonabstract methods, which are usually instance of the template method pattern. All the methods of an interface are abstract,
Whether or not this declaration is explicit.
(3) An abstract class can declare instance variable that its subclass inherit. An interface cannot declare instance variables, although it can establish static final fields.
(4) An abstract class can define constructors, an interface cannot.
(5) An abstract class can have methods whose visibility is protected, private, or none (Package); every method in an interface must be public.
(6) An abstract class inherits form object, including such method as clone () and equals ().
II. The functions of interfaces are mainly reflected in the following aspects:
(1)The same behavior of irrelevant classes can be implemented through the interface, without understanding the class corresponding to the object.
(2)You can specify the methods to be implemented by multiple classes through the interface.
(3)You can use interfaces to understand the interaction interface of objects without understanding the classes corresponding to objects.
3..Some interface questions:
1What you see in the book is:JavaOnly constants and empty methods are defined in the program interface. What is the use of empty methods? I also want to write the method body in the class. What should I do with the interface.
Answer::The interface defines the prototype of the class method, but it cannot be a null method, because the empty method means that there is an implementation body, but the implementation body is a null operation. In fact, the interface does not define any implementation bodies. The specific implementation body is in the class that implements the interface. The interface only defines the call methods of these methods.
You can also write methods directly in the class without using interfaces. However, if your methods need to be implemented in many classes, you can abstract them into an interface specification, isn't it better?
2, Written in a program2And write the method body in the same class. What is the relationship between this and multi-inheritance.
Answer::A class describes an object, which may be a complex object and has many actions. If you classify these actions, you can use interfaces.ADefine a group of actions and interfacesBDefine another group of actions. This structure is clear.
This method has the advantages of multi-inheritance and avoids the defects of Multi-inheritance. In fact, in history, interfaces are designed to solve various problems brought about by multi-inheritance.
3, The interface defined in the package, how to know what method is defined in it.
Answer::The interface defines the input and output of methods. These are all protocols, and the specific implementation is in each class. For many places that only need abstract interfaces, you do not need to know what the specific class is, as long as the class implements this interface.