Java Summary 2 polymorphic issues and container introductions

Source: Internet
Author: User
Tags introductions

Object-oriented this thing, in fact, we have not felt that they are in use, after I must use. I used to learn C # seriously once, recently studied Java. Look again.

Polymorphic problems:

Polymorphic (1) Compile-time polymorphism and (2) runtime polymorphism in Java

(1) Compile-time polymorphism is easier to understand: in fact, is through the method overload, is the method of overloading, the same function name but can be different parameters. This is the overload (so easy)

(2) Runtime polymorphism: This is achieved through method overrides, that is, when the subclass inherits the parent class, overrides the parent class method by overriding the method.

Simply put: For example, we have a parent class A, subclass B inherits a,c,d through extends, and we just call a when we call a method.

(using a parent class reference to a child class object, when calling a method in a parent class, different subclasses show different method results, which is polymorphic, with inheritance only polymorphic)

Role: Good extensibility, when we add a class, as long as the parent class, and overwrite the parent class method is OK. Multiple implementations of an interface, the performance of inheritance is polymorphic.

(The application does not have to write a functional call to each derived class, only the abstract base class can be processed)

I'll add the reason for using the interface: It allows us to create more generic code by writing code for interfaces rather than implementing classes, so that our code can apply more object types.

Container:

We use more certainly is list<t> list=new Arraylist<t>,, undeniable ArrayList is very powerful, but in fact Java has a lot of other good things.

The first thing to illustrate is that list is an interface, not a list x=new list (); list just declares the function, and it is not implemented. ArrayList is a concrete implementation class that inherits the list.

(1) array: when it comes to the container, it is possible to refer to an array , let's say that the array is fixed by the size of the container (in fact, it is not a container), and then because the array size is known, so the parameter subscript can be randomly accessed , The speed should be the fastest O (1), one thing to note is that the legths of the array is the size of the array rather than the number of data inside the array.

(2) The difference between collection and collections

Collection is the top- level interface for the collection class, and it also has a list and set two interfaces, (Interface inheritance interface)

Collections is a tool class for a collection class that provides a series of static functions that can be used to sort the collection, choosing the maximum value ... .....

(3) Diagram to illustrate the relationship (and map, it is and collection a level, including hashtable,hashmap .... )

(4) iterator (iterator)

Iterators are a design pattern, which is also a design pattern. We know that iterators are used to pick the collection object, and we can manipulate the object directly by selecting the object reference from the iterator. I won't post the code.

Role: As a design pattern, you can apply different types of containers without having to know or rewrite the code.

This may not be understandable, so to speak: is that we can not control how the collection is implemented, whether it is a list or map, I just need to call the iterator is OK. In fact, the general time also can not be used, may be in your

To change the collection implementation method of a program to use.

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.