Java object-oriented-encapsulation

Source: Internet
Author: User

Encapsulation: is to surround the process and data, access to the data only through the defined interface. Object-oriented computing begins with this basic concept that the real world can be portrayed as a series of fully autonomous, encapsulated objects that access other objects through a protected interface. Encapsulation is an information hiding technology that is encapsulated in Java through the keyword private. (Field privatization)

What is encapsulation? Encapsulation combines all the components of an object, encapsulates the data that defines how the program references the object, encapsulates the fact that it hides the data of the class by using methods, and controls how much the user modifies the class and accesses the data.

Examples of encapsulation:

public class man{//encapsulation of attributes, a person's name, age, wife are the private property of this object (person) name;private int age;private Woman wife;// The packaging of the person's way to the outside world can be set by the wife, name, age, and the name and age of a man.
Method encapsulation
public void Setwife (Woman wife) {
This.wife = wife;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
}
public class Woman {
Attribute Encapsulation
private String name;
private int age;
Private man husband;
Method encapsulation
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
Public Mans Gethusband () {
return husband;
}
public void Sethusband (mans husband) {
This.husband = husband;
}
}/*** look closely, you will find that the man does not provide Getwife method, because men do not want their wives to be visited by the outside world, * * Next, is the encapsulation can be a property of an object private, and provide some of the properties can be accessed by external methods, * * For example, the Name property, Both the man and the woman classes have corresponding get and set methods, which can be accessed and modified by the outside world * * While some objects do not want the external access properties, do not provide its methods, such as the man's wife property, there is no get method * * The outside world is unable to get the wife attribute of the man class **/above that example may not highlight the benefits of encapsulation, below a super simple expression under: public class Show{public static void Show (String str) { System.out.println (str);}} Above is the encapsulation of System.out.println (); When called: public class Use{public static void Main (string[] args) {show.show ("encapsulation");}} The role of encapsulation: ① The data encapsulation feature of the object completely eliminates the problems caused by the separation of data and operation in the traditional structure method, improves the reusability and maintainability of the program, and reduces the burden of the programmer to keep the data and operation content. 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.

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.