Three major Java features: encapsulation, inheritance, polymorphism, and three major Polymorphism

Source: Internet
Author: User

Three major Java features: encapsulation, inheritance, polymorphism, and three major Polymorphism

I. Encapsulation

Encapsulation is to classify the commonalities (including attributes and methods) of the same class into a class for convenient use.

Concept: In the object-oriented programming method, Encapsulation (Encapsulation) refers to a method to package and hide the Implementation Details of abstract interfaces, it can be considered as a protection barrier to prevent the code and data of this class from being randomly accessed by the Code defined by the external class. To access the code and data of this class, it must be controlled through strict interfaces.

Function: 1. Achieve a professional division of labor. After the code that can implement a specific function is encapsulated into an independent entity, Each programmer can call it as needed and modify it conveniently;

2. Convenient control. If the program has a problem or needs to be updated, you can directly modify it in this class, instead of modifying the program fragment of the called code;

3. Hide information and implement details. By controlling access permissions, you can hide information that you do not want the client programmer to see. For example, if the password of a customer's bank needs to be kept confidential, you can only develop permissions for the customer.

 

Ii. Inheritance

1. Concept: Inheritance. New Attributes and methods are added to the original class to construct the technology of the new class. A Class inherits from another Class. It is called the inherited Class as a Sub Class and an inherited Class. The inherited Class is the parent Class Super Class, and also called a superclass. Private Members cannot be inherited, and constructors cannot.
Syntax: when defining a subclass: class subclass name extends superclass {
New member of subclass
}
2. Features: When a subclass object is created, a parent class object is created first, but the parent class object cannot be called because there is no object variable to receive the parent class object. The subclass can write its own attributes and methods, the purpose is to implement function expansion. sub-classes can also rewrite the method of the parent class, that is, method rewriting.

3. Role: Code reuse.

4. this and super:Read-Only variables in java can only be used and cannot be modified. This indicates the current object, this class, and the local constructor. super indicates the parent object, parent class, and parent constructor.

The parameters and domain names of the method or constructor can be duplicated, but this must be used, for example:

OverRide: overwrites a property to redeclare it. overwrites a method to modify the content of a method.

 

3., Polymorphism

1. concept:Polymorphism, that is, multiple forms. An object belongs to multiple classes. Based on the context and actual needs, it dynamically determines the class to belong to and the method of the same name in the class to be executed. For example, a pet has many different expressions or implementations, such as a kitten, a puppy, or a lizard. Then I went to the pet store and said, "Please give me a pet." The waiter can give me a kitten, a puppy, or a lizard. Then we say that the "Pet" object has polymorphism.

2. conditions:1) Inheritance; 2) overwriting; 3) the declared parent class Object references an object pointing to the subclass;

3. Features: 1) The Same Thing calls the same method, and the parameters are the same, but the behavior is different;

2) The concept of polymorphism is developed based on encapsulation and inheritance;

3) Child classes appear as parent classes, but they are also implemented in their own way when doing things. Sub-classes appear as parent classes and need to be transformed upwards (upcast). The upward transformation is automatically implemented by JVM and is secure, but downcast is insecure, forced conversion is required. When a subclass appears as a parent class, its own attributes and methods cannot be used.

 

Appendix:A piece of code that reflects the three major java features at the same time

 

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.