Java object-oriented (class, attribute, method, package, encapsulation, inheritance, polymorphism)

Source: Internet
Author: User
Document directory
  • Four encapsulated Permissions
  • Four encapsulated Permissions

.

### Encapsulation

Encapsulation

1. encapsulation is to privatize attributes and provide public methods to access private attributes. The carrier that encapsulates the attributes and behaviors of objects is classes. Classes usually hide the Implementation Details of objects, this is the idea of encapsulation.

Encapsulation in life, such as a lamp, you only need to know how to turn on the switch, the light is on, and the lamp is off, but you do not know the principle, because the complicated lines are encapsulated by the beautiful appearance of the desk lamp.

2. Why is encapsulation required?

Through encapsulation, data access to attributes can be restricted, and the maintainability of the program is increased.

Because the value method and value assignment method hide the implemented changes, the classes that read or modify this attribute are not affected. This avoids large-scale modifications and enhances the maintainability of the program.

Public class demo {
Private int I; // Private Property
Private string STR;

// Public property method and access private property
Public int Geti (){
Return I;
}
Public void SETI (int I ){
This. I = I;
}
Public String getstr (){
Return STR;
}
Public void setstr (string Str ){
This. Str = STR;
}


}

Four encapsulated Permissions

1. PRIVATE: if the member variables and member methods of this class are modified using private, the member variables or member methods can only be used in this class, classes in subclass or other packages are invisible.

2. By default, this permission cannot be transferred across packages. This member's face change or member method can only be called in this class or its subclass.

3. protected by protected, if the member variable or member method is modified by protected, the member variable or member method is visible to the class or subclass of the same package. If you want to cross-package, it must be visible in the subclass.

4. Public public: if the member variables or member methods are modified using public, they are visible anywhere.

The permission levels of these four permission modifiers are low ----------> high

Private-> default-> protected-> Public

### Inheritance

### Polymorphism

.

.

.

.

.

.

.

.

.

Encapsulation

1. encapsulation is to privatize attributes and provide public methods to access private attributes. The carrier that encapsulates the attributes and behaviors of objects is classes. Classes usually hide the Implementation Details of objects, this is the idea of encapsulation.

Encapsulation in life, such as a lamp, you only need to know how to turn on the switch, the light is on, and the lamp is off, but you do not know the principle, because the complicated lines are encapsulated by the beautiful appearance of the desk lamp.

2. Why is encapsulation required?

Through encapsulation, data access to attributes can be restricted, and the maintainability of the program is increased.

Because the value method and value assignment method hide the implemented changes, the classes that read or modify this attribute are not affected. This avoids large-scale modifications and enhances the maintainability of the program.

Public class demo {
Private int I; // Private Property
Private string STR;

// Public property method and access private property
Public int Geti (){
Return I;
}
Public void SETI (int I ){
This. I = I;
}
Public String getstr (){
Return STR;
}
Public void setstr (string Str ){
This. Str = STR;
}


}

Four encapsulated Permissions

1. PRIVATE: if the member variables and member methods of this class are modified using private, the member variables or member methods can only be used in this class, classes in subclass or other packages are invisible.

2. By default, this permission cannot be transferred across packages. This member's face change or member method can only be called in this class or its subclass.

3. protected by protected, if the member variable or member method is modified by protected, the member variable or member method is visible to the class or subclass of the same package. If you want to cross-package, it must be visible in the subclass.

4. Public public: if the member variables or member methods are modified using public, they are visible anywhere.

The permission levels of these four permission modifiers are low ----------> high

Private-> default-> protected-> Public

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.