Java interface-Oriented programming understanding

Source: Internet
Author: User

Beginners may be in the study will have a lot of doubts, why so, clearly can be realized, the benefits of doing so what?

People who may think it is easy to understand, and may even think that the question is mentally retarded, but for small white may be a hard-thinking can not be solved.

Himself as a beginner also know that beginners are not easy, many questions on-line no very clear answer.

He encountered any problems to write down after it, but also to facilitate yimeimei, after all, the ancestors planted trees posterity.

At the beginning of the container, there are many questions, why do you use the interface as the type to implement? (list<lnteger> L = new arraylist<> ())

Rather than just writing it? (ArrayList <Integer> i = new arraylist<> ())

I would like to say that my beginner's understanding, if there are errors in the point of looking, thank you!

At this point a interface can be regarded as a function to complete a specification, the implementation of its A1 class is to complete a function of a method, A2 class is to complete a function of a method.

Then we need to implement a function, we need to use different methods to achieve this function depending on the situation.

This idea is more in line with our everyday object-oriented thinking, first think about what needs to be done, and then how to complete this function (according to the current situation to choose the appropriate method to complete).

For example, I need to store the ordered data here, then we want to implement this function first, of course, the list interface.

Then, based on my current situation, I think the list interface is more suitable for the current situation.

is the array (ArrayList) or the list (linkedlist) stored?

This requires a combination of the advantages and disadvantages of these two storage methods and the current specific needs to decide.

To give a more popular example:

If I define the function of an interface to go to the library , I define a few classes that implement the interface,

There are teaching buildings to go to the library class , there are canteen to the library class , and the bedroom to the library class .

And what do we want to do first when we think about it? to the library , this determines that the interface is the interface to the library .

And then we want to go, because of the individual location of the different, we need to according to the current location of the specific situation

To call the appropriate class , I in the dorm room to the library class , I called the canteen in the canteen to the library class .

If the school is enlarged, a few more campus districts. to the library this interface We do not need to change.

Just add an implementation (to the Library Interface) (campus area to the library Class) on it . (Class implementation Interface)

Such a specific function determines that, even if there is a new situation that needs to be done, we just need to add a new class.

Because the establishment of the function is basically the same, it is universal.

Interface is an abstract broad concept, more like a macroscopic thing.

First determine the macro-level of things (interfaces), and then think of how the micro-specific implementation (Implementation of the interface Class), the following new micro can be directly added, because the micro is also subordinate to this macro.

Java interface-Oriented programming understanding

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.