What is the interface-oriented programming that is raised by the list and ArrayList in Java?

Source: Internet
Author: User
Tags modifier

What you already know is as follows:
    1. What is a constructor function?
      • A Java constructor, also called a constructor, is a special method in a Java class, with the same method name as the class name, which is typically used to initialize member variables, and when the object (instance) of a class is to be generated, the constructor is called, if the constructor of the declaring class is not displayed in the class, the Java A default empty constructor with no parameters is generated automatically. (Interface classes and abstract classes have no constructors)
    2. What is an interface?
    • An interface is a collection of functions, which can also be seen as a type of data and a special abstract class. The methods in the interface are all abstract, and it makes no sense to call the new interface directly, nor does Java allow it. Of course, the interface can also be implemented, the class that implements the interface needs to rewrite the method in the interface, and complete the specific logic (whether it manifests polymorphism?). )
    • Features of the members in the interface:
      • The variable is defined in the interface, but the variable must have a fixed modifier decorated--public static final, so the variable in the interface is also called a constant (which needs to be compared with ENMU) whose value cannot be changed
      • The method is defined in the interface, and there is also a fixed modifier--public abstract
      • Interface cannot create object
      • Subclasses must overwrite all the abstract methods in the interface (Implement interface methods), can instantiate, subclasses can also have their own methods, but through the interface point to create the instance cannot call the subclass of the method, which is the disadvantage of polymorphism, can not directly access the members and methods of the subclass unique
    • Interface idea: Convenient later maintenance, only need to care about the method in the interface. A large number of interfaces are used in the collection system
    • The advantages of the interface: the relationship between the ① class and the interface, the implementation of the relationship, and is a multi-implementation, breaking a class can only inherit a parent class limit; ② provide rules ③ reduce the coupling of the program (can achieve modular development, each person to achieve their own modules, improve development efficiency)
    • Differences between interfaces and abstract classes: see notes for an in-depth understanding of abstract class and interface
Back to the chase.
    1. The list is an interface, and the interface cannot be instantiated (without constructors), and ArrayList is an implementation class for the list interface. We can create an object reference to our list interface as follows, and the instance object of the ArrayList implementation class acts as an object reference to the list interface.
list<string> list = new arraylist<string> ();(JDK1.7 ArrayList Capacity and capacity expansion: initial 0, add an object after 10, each expansion to (raw capacity *3/ 2) and (the length of the array + 1) between the larger values)
Lead: This is a simple example of interface-oriented programming, but also an important object-oriented knowledge point. When we were learning oop, the most important aspect of object-oriented was polymorphism (method overloading and method coverage), and we all knew that interfaces and abstractions could not be instantiated, but they could create an object reference to themselves (why point to themselves, to implement interface programming), Their implementation class or subclass is in this role, which is also the advantage of polymorphism in object-oriented programming (not rigorous, because the nature of interface and polymorphism is not at a level, polymorphism is one of the object-oriented features, and the interface is a series of methods of Declaration, interface can reflect polymorphism, but polymorphism does not necessarily need to use the interface, In short, you can use code to define an interface in the program, polymorphism is just a description of the characteristics of the code, a conceptual abstraction and summary.

What is the interface-oriented programming that is raised by the list and ArrayList in Java?

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.