Java interface learning notes

Source: Internet
Author: User

 

I. Basic interface knowledge 1. Java does not support multiple direct parent classes (Multi-inheritance) for a class, but multiple interfaces can be implemented, indirectly implements Multi-inheritance. 2. Interface-related design patterns: 1. customizes service patterns to design fine-grained interfaces. Each interface represents a set of services and creates a composite interface through inheritance. 2, adapter mode when the interface between each system does not match, use the adapter to convert Interface 3. The default adapter mode provides a simple default implementation for the interface 4. The proxy mode creates a proxy class for the interface implementation class, the user obtains the service implementing the class through the proxy. 5. The identification type mode uses interfaces to identify an abstract type without any behavior. 6. The constant interface mode defines static constants in the interface, introduce these constants 3 through the import static statement in other classes. The features of the interface are summarized as follows: 1. All member variables in the interface are public, static, and final by default ), initialization must be displayed, that is, the member variables in the interface are constants (uppercase, separated by "_"). 2. All methods in the interface are Is public, abstract type (can be omitted), no method body, cannot be instantiated public interface a {int const = 1; // valid, const is public, static by default, final type void method (); // valid; Method () is public by default, abstract type public abstract void method2 (); // method2 () The display declaration is public, abstract type} 3, the interface can only contain public, static, final member variables and public, abstract member method public interface a {int var; // error, vaR is a constant and must display the initialization void method (){...}; // error. The interface can only contain the abstract method protected void method2 (); // Error The method in the interface must be public Type Static void method3 (){...}; // error. The interface cannot contain static methods.} 4. The interface does not contain constructor. Public interface a {public a () {...} cannot be instantiated (){...}; // error. The interface cannot contain the constructor void method () ;}5. One interface cannot implement (implements) Another interface, but it can inherit multiple other interfaces: public interface a {void methoda ();} public interface B {void methodb ();} public interface C extends, B // C is called the Composite Interface {void methodc ();} public interface c implements {...} // Error 6: The interface must use classes to implement its abstract method pub LIC Class A implements B {...} 7. When a class implements an interface, it must implement all the abstract methods in the interface. Otherwise, the class must be declared as abstract 8 and cannot create an interface instance (instantiate ), however, you can define reference variables of the interface type. This reference variable references the public class B implements a {} A = new B () instance of the class implementing this interface (); // The reference variable A is defined as the interface type, and instance B A = new A () is referenced; // error, the interface cannot be instantiated 9, A class can only inherit one direct parent class, but multiple interfaces can be implemented, indirectly implementing multi-inheritance. public Class A extends B implements c, d {...} // Class B, C, and D are interface4. Through interfaces, you can easily abstract existing systems from the bottom up. For any two classes, no matter whether they belong to the same parent class or not, only if they have the same function Abstract An interface type. the existing Inheritance tree can easily abstract new interfaces from the class, but it is not so easy to abstract new abstract classes from the class, therefore, interfaces are more conducive to the maintenance and reconstruction of software systems. for two systems, interface interaction is better than abstract class interaction. 5. interfaces are an important magic weapon for building loosely coupled software systems. Because interfaces are used to describe all services provided by the system externally, member variables and methods in interfaces must be public, make sure that external users can access them. interfaces only describe what the system can do, but do not specify how to do it. Methods in all interfaces are abstract methods. Interfaces do not involve details related to any specific instances, therefore, the interface does not have a constructor, cannot be instantiated, and no instance variable. 2. Comparison between abstract classes and interface 1. abstract classes and interfaces are both located at the same level of the inheritance tree. 1 represents the abstraction layer of the system. When a system uses a class on the inheritance tree, we should try to declare the referenced variable as the upper-layer abstract type of the inheritance tree. This can improve the coupling between the two systems. 2 is not allowed to be instantiated. 3 is included in the abstract method, these abstract methods are used to describe the system Which services can be provided by the system, but do not provide specific implementation differences: 1. In the abstract class, you can provide default implementation for some methods to avoid repeated implementation in the subclass. This is the advantage of the abstract class, however, this advantage limits multi-inheritance, and the interface can only contain abstract methods. because a specific method can be added to an abstract class, the function of extending the abstract class is to add a specific method to the abstract class without affecting its subclass. For interfaces, once an interface is published, it must be very stable, because adding abstract methods to the interface will affect all the real-time classes. These Implementation classes either implement new abstract methods, either declared as abstract class 2, a class can only inherit one direct parent class, this parent class may be an abstract class, but a class can implement multiple interfaces, This is the advantage of the interface, however, this advantage is three steps that do not allow implementation for any method. Why does the Java language not allow multiple inheritance? When the subclass overwrites the instance method of the parent class or hides the member variables and static methods of the parent class, Java virtual machine uses different binding rules. If a class can have multiple direct parent classes, therefore, to simplify the system structure design and dynamic binding mechanism, the Java language prohibits multiple inheritance. only abstract methods, instance variables, and static methods in interfaces can be implemented only by interface implementation classes (Abstract METHODS In interfaces are implemented through classes ), therefore, even if a class has multiple interfaces, it does not increase the complexity of dynamic binding on the Java Virtual Machine. because the Java Virtual Machine will never bind a method to an interface, but will only bind a method to its implementation class. iv. General principles of using interfaces and abstract classes: 1. Using Interfaces as the window for system interaction with the outside world stands in the perspective of the outside user (another system, the interface promises to the user what services the system can provide. From the perspective of the system itself, the interface determines which services the system must implement. The interface is the high-level abstract type in the system. through interface interaction, the send coupling system a between two systems can interact through system B. This means that when system a accesses system B, it declares the referenced variable as the interface type in system B, the referenced variable references the implementation class instance of the interface in system B. public interface B {} public class c implements B {} public class A {} B A = new C (); 2. The interface itself must be very stable. Once the interface is formulated, it is not allowed to meet any requirements. Otherwise, it will affect the external users and the system itself. 3. Use an abstract class to customize the extension point abstract class in the system for partial implementation, some other functions are also implemented through its subclass 2008/1/9. The binding rules in the Java polymorphism mechanism are further analyzed in class base {string Var = "basevar "; // instance variable static string staticvar = "staticbasevar"; // static variable void method () // instance method {system. out. println ("base method");} static void staticmethod () // static method {system. out. println ("static base method") ;}} public class sub extends base {string Var = "subvar"; // instance variable static string staticvar = "staticsubvar "; // static variable void method () // hide the method () method of the parent class {system. out. println ("sub method");} static void staticmethod () // hide the staticmethod () method of the parent class {system. out. println ("static sub method");} string subvar = "Var only belonging to sub"; void submethod () {system. out. println ("method only belonging to sub");} public static void main (string ARGs []) {// reference variable who is declared as base type, reference the instance base of the sub class who = new sub (); // member variables (static variables, instance variables) and types declared by the referenced variables (base type) bind the member variable to system. out. println ("who. var = "+ who. vaR); // Therefore, print the VaR Variable System of the base class. out. println ("who. staticvar = "+ who. staticvar); // Therefore, print the staticvar variable of the base class // bind the instance method to the method of the object actually referenced by the referenced variable (sub object) to who. method (); // Therefore, print the method () method of the sub instance // The static method is bound to the method of the type (base type) declared by the referenced variable. staticmethod (); // Therefore, print the staticmethod () method of the base class} [analysis process] 1. For a variable of the reference type, the Java compiler processes the data according to the declared type. for example, in the following code, the compiler considers who is a reference variable of the base type, and does not have a subvar member variable. If the submethod () method is used, the compiling error base who = new sub () is returned (); // The reference variable who is declared as the base type and references the instance who of the sub class. subvar = "123"; // compilation error. The subvar attribute who is not in the base class. submethod (); // compilation error. There is no submethod () method in the base class. If you want to access the sub class members, you must use the forced type conversion: base who = new sub (); // forcibly convert the WHO member variable of the base reference type to the sub reference type // The type of the referenced variable to the subclass is called the downward transformation, the type for converting referenced variables to parent classes is called upward Transformation (sub) WHO ). subvar = "123"; (sub) WHO ). submethod (); the Java compiler allows type conversion between classes with direct or indirect inheritance relationships. For upward transformation, the Java compiler automatically performs and for downward transformation, forced type conversion is required. If the two types do not continue, that is, they are not in the same inheritance branch of the inheritance tree, the Java compiler does not allow type conversion 2, for a variable of the reference type, the Java Virtual Machine processes the variable according to the object actually referenced by it at runtime. For example, although the following code can be compiled, however, classcastexception is thrown during runtime. The exception base who = new base (); // who references the base class instance sub S = (sub) WHO; // during runtime, classcastexception is thrown. during runtime, The subclass object can be converted to the parent class, while the parent class object cannot be converted to the subclass Class 3. In the runtime environment, when you use a reference type variable to access the methods and attributes of the referenced object, Java virtual machine uses the following binding rules: 1. the instance method is bound to the method of the object actually referenced by the referenced variable, this kind of binding is dynamic binding, because it is dynamically determined by the Java Virtual Machine at runtime. 2. Static methods are bound to methods of the type declared by reference variables. Such binding is static binding, because it is actually bound to 3 in the compilation phase, member variables (static variables, instance variables) are bound to the member variables of the type declared by the referenced variables. Such binding is static binding, because it is actually bound at the compilation stage.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.