Java interface learning notes

Source: Internet
Author: User

Java does not support multiple direct parent classes (Multi-inheritance) for a class, but it can implement multiple interfaces (implements) to indirectly implement multi-inheritance.

 

I. Basic knowledge about Java Interfaces

1. Java does not support multiple direct parent classes (Multi-inheritance) for a class, but multiple interfaces (implements) can be implemented, indirectly implementing multi-inheritance.

2. design patterns related to Java interfaces:

1. Customize the Service Model

Design fine-grained interfaces. Each Java interface represents a set of related services and creates a composite interface through inheritance.

2. Adapter Mode

When the interfaces of each system do not match, use an adapter to convert the interfaces.

3. Default adapter Mode

Provides simple default implementation for interfaces

4. Proxy Mode

Creates a proxy class for the Implementation class of the Java interface. The user obtains the service of the implementation class through the proxy.

5. ID type mode

Identifies an abstract type with no behavior using interfaces

6. Constant interface mode

Defines static constants in the interface, and introduces these constants in other classes through the import static statement.

Java interface feature induction:

1. By default, all member variables in the Java interface are public, static, and final (can be omitted). They must be displayed for initialization, that is, the member variables in the interface are constants (uppercase, words are separated)

2. The methods in the Java interface are public by default, abstract type (can be omitted), no method body, and cannot be instantiated

 
  1. Public interface
  2. {
  3. Int const = 1; // valid. The default const type is public, static, and final.
  4. Void method (); // valid; Method () is public by default, abstract type
  5. Public abstract void method2 (); // method2 () display declaration as public, abstract type
  6. }

3. Java interfaces can only contain public, static, final member variables and public, abstract member Methods

 
  1. Public interface
  2. {
  3. Int var; // error. var is a constant and must be initialized.
  4. Void method () {...}; // error. The interface can only contain abstract methods.
  5. Protected void method2 (); // error. The method in the interface must be of the Public type.
  6. Static void method3 () {...}; // error. The interface cannot contain static methods.
  7. }

4. The interface cannot be instantiated because there is no Constructor

 
  1. Public interface
  2. {
  3. Public A () {...}; // error. The interface cannot contain constructor.
  4. Void method ();
  5. }

5. One interface cannot implement another interface, but it can inherit multiple other interfaces.

 
  1. Public interface
  2. {
  3. Void methoda ();
  4. }
  5. Public interface B
  6. {
  7. Void methodb ();
  8. }
  9. Public interface C extends a, B // C is called a Composite Interface
  10. {
  11. Void methodc ();
  12. }
  13. Public interface c implements a {...} // Error

6. Java interfaces must use classes to implement their abstract methods.

Public Class A implements B {...}

7. When a class implements a Java interface, it must implement all abstract methods in the interface; otherwise, the class must be declared as abstract

8. You are not allowed to create an interface instance (instantiation), but to define reference variables of the interface type. This reference variable references instances of classes that implement this interface.

 
  1. Public Class B implements {}
  2. A A = new B (); // reference variable A is defined as a interface type and B instance is referenced
  3. A A = new A (); // 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 {...} // B is class, c, d is Interface

4. Through interfaces, you can easily abstract existing systems from the bottom up. For any two classes, whether they belong to the same parent class or not, they only 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 of abstract classes and interfaces

1. abstract classes and interfaces are both on the upper layer of the inheritance tree.

Similarities

1. It 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 abstraction type of the inheritance tree, this improves the transmission coupling between the two systems.

2, cannot be instantiated

3. All include abstract methods. These abstract methods are used to describe the services that the system can provide, but do not provide specific implementations.

Differences:

1. Some methods can be implemented by default in the abstract class to avoid repeated implementations in the subclass. This is an advantage of the abstract class, but this advantage limits multi-inheritance, 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 the interface is published, it must be very stable, because adding abstract methods to interfaces will affect all implementation classes. These Implementation classes can either implement new abstract methods or declare them as abstract classes.

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, which is an interface advantage, 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, this makes binding rules more complex,

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. 4. General principles of using interfaces and abstract classes:

1. The interface is used as the window for system interaction with the outside world. The interface promises to the user what services the system can provide, from the perspective of the system itself, interfaces define the services that must be implemented by the system. interfaces are the high-level abstraction types 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, this reference variable references the implementation class instance of the interface in system B.

 
  1. public interface B  
  2.  {  
  3.  }  
  4.  public class C implements B  
  5.  {  
  6.  }  
  7.  public class A  
  8.  {  
  9.  }  
  10.  B a = new C(); 

2. the Java interface itself must be very stable. Once the Java interface is formulated, it is not allowed to meet any requirements. Otherwise, it will affect the external users and the system itself.

3. Use abstract classes to customize extension points in the system

Abstract class to complete partial implementation, but also some functions through its subclass to achieve

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.