Java Basics (i)-----The three main features of Java object-oriented encapsulation

Source: Internet
Author: User

the three main features of Java object-oriented encapsulation:  Encapsulation (encapsulation) is an important feature of object-oriented, which is to combine the properties of an object and the operation (or service) into a separate whole, and hide the inner implementation details of the object as much as possible.  the so-called encapsulation is the bundling of attributes and methods together, encapsulated into an object,form an indivisible independent unit and hide the internal structure of the object as much as possible, that is, if we use encapsulation technology, others can only use what we do to see the internal structure of the thing we do.  If we describe a person (assuming people as a class), we can use name, age, gender and so on to describe, if we do not encapsulate we need three variables to describe him, in object-oriented can use a person class to encapsulate these attributes, a person has three member variables, Name,age,sex, The 3 properties are created whenever an object of such a class is generated.you can think of it that way .For example a computer mainframe, he is encapsulated, you can change his state in some ways, and do not need to understand his principles, such as I press the power button, I will know that I press this button, the computer will start, and do not need to know how he runs  for encapsulation, an object encapsulates its own properties and methods, so it does not need to rely on other objects to do its own work.   What is encapsulation?

Encapsulation is the privatization of properties that provide public access to private properties.

This is done by modifying the visibility of properties to restrict access to properties, and creating a pair of value (getter) methods and assignment (setter) methods for each property for access to those properties.

such as: private String name;

Public String GetName () {

Return

}

public void SetName (String name) {

This.name=name;

}

Why encapsulation, encapsulation What is the role?

Through encapsulation, it can realize the restriction of the data access to the attribute, and increase the maintainability of the program.

Because the value method and the assignment method hide the implemented changes, it does not affect the class that reads or modifies the attribute, avoids the large-scale modification, and the maintainability of the program is enhanced.

The object's data encapsulation feature also separates the private data of the object from the public data, protects the private data, reduces the possible inter-module interference, and achieves the purpose of reducing the complexity of the program and improving controllability.

Package benefits and specific implementation steps

* Steps to implement encapsulation:

(1) Modify the visibility of the property to restrict access to the property.

(2) Create a pair of assignment methods and value methods for each property to access these properties.

(3) In the assignment and value method, add access restrictions to the attribute.

* * In order to achieve good encapsulation, we usually declare the member variable of the class as private, and then access the variable through the public method. For the operation of a variable, there are generally read and assignment operations, we define two methods to achieve both operations, a getxxx () (XXX represents the name of the member variable to access), to read the member variable operation, and the other is setxxx () is used to assign a value to this member variable. * * If the outside program can arbitrarily modify the member variables of a class, it will cause unpredictable program errors, like a person's height, can not be arbitrarily modified outside, only through a variety of nutrition methods to modify this attribute.

public class accpteacher{
private String name;
private int age;
Public String GetName () {
return name;

}
public void SetName (String name) {
THIS.name = name;

}
public int getage () {
return name;

}
public void SetName (String age) {
if (age>200) {
System.out.println ("Age does not fit, too big!") ");
}else{
This.age = age;

}
}


Advantages of Encapsulation:

(1) Hide the implementation details of the class, good encapsulation can reduce the coupling;

(2) so that users can only through the pre-custom good method to access data, can easily add control logic, limit the unreasonable operation of the attribute, to ensure the security of the program;

(3) Easy to modify, enhance the maintainability of the Code;

Reference: http://blog.csdn.net/chenssy/article/details/12757911 (learn a lot of thanks here)

I learn to summarize the use, if there are mistakes errors, please correct me, set an open-minded learning to correct! Thank you.

do not find excuses for failure, only to find a way to success!!!

Java Basics (i)-----The three main features of Java object-oriented encapsulation

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.