Java encapsulation and polymorphism

Source: Internet
Author: User

encapsulation, integration, polymorphism, and abstraction are fundamental features of Java.

The first step in encapsulation is to assemble the class, which is to define a class, and consider the properties, methods, etc. of the class. The second step is the hiding of information, which includes access modifiers, Get/set methods, and implementations of certain methods. When using the private modifier to provide the Get/set method, there are some advantages:

1, can only provide get or set method, the property becomes read-only or write-only;

2, the Get method can provide authentication of permissions;

3. The Set method can provide validation of the validity of the data;

4, can hide the internal properties of the storage mode;

Polymorphism refers to the same behavior, different implementations. It is also divided into static polymorphism and dynamic polymorphism, in which dynamic polymorphism is the essence of Java object-oriented. Static polymorphism is implemented by overloading of methods, dynamic polymorphism is realized by means of rewriting and dynamic binding, and dynamic polymorphism must occur in inheriting class relationships.

The technical basis for polymorphism is as follows:

1, Upward transformation Technology: A parent class reference can point to different sub-class objects;

2, instanceof keyword: used to judge the real type of the runtime object, is to ensure that the code runs without the crash of the important barrier;

3. Dynamic Binding Technology: If Class A is to invoke Class B objects, the Class B's parent class should be bound as much as possible. If the method of the parent class is overridden in a subclass, the method that is overridden in the subclass is executed when executing the program, rather than calling the method in the parent class, which increases the richness. The overridden method in the parent class is actually hidden.

Java encapsulation and polymorphism

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.