Java basics 04 encapsulation and interfaces

Source: Internet
Author: User

Up to now, data members and methods have been both open to internal and external entities. We can use this inside the object to call the data members and methods of the object. When we call an object in another class (that is, when it is external), we can use the object. Data member and object. Method () to call the data member and method of the object.

We will encapsulate the members (including data members and methods) of the (encapsulation) object, so that only the members of the part can be called from the outside. With encapsulation, we can improve the ease-of-use and security of objects.

 
Encapsulation and Interface
Encapsulation (encapsulation) is a commonly used term in computers. It retains limited external interfaces to hide specific implementation details. For example, in the Linux architecture, we can see that the Linux operating system encapsulates the details of the underlying hardware, and only retains the interface called by the system. The user is outside the encapsulation and can only perform the required operations through interfaces.

 

Encapsulation is common in life. For example, the following is a charging record:

 

Even if a user does not read the manual, he can also guess the operation of the Supervisor: Switch and charge. This utility uses a plastic shell to hide the internal details that the user does not need to contact. It only keeps two interfaces, switches and electrical plugs. These two interfaces are sufficient for users to use the functions that the product wants to implement in design. If all the details are exposed to the user at the same time, the user will feel overwhelmed by the product (such as the following remote control without shelling ). Therefore, encapsulation improves the usability of the product.

 

 

If the product is not encapsulated, many details of the valve or remote control will be exposed to the user: battery, circuit, and sealed rubber. Although this allows users to perform product operations more freely, such as directly discharging the battery and extracting an LED lamp. However, users are often at risk of damages to products. Therefore, encapsulation improves product security.

 

A Java software product is the same as a daily product. An object can have many members (data members and methods ). Some data members and methods are only intended for internal use. At this time, we hope to have a mechanism to "shell" the Java object, so as to encapsulate the interface of the object. In this way, users of this Java product can easily learn external interfaces and avoid access to unnecessary internal members.

 

Encapsulation of object members
Java uses three keywords to control the external visibility (visibility) of object members: public, private, and protected. Public indicates that the member is externally visible, that is, the Member is part of the interface. private indicates that the member is externally invisible and can only be used internally and cannot be accessed from outside.

(Protected involves the concept of inheritance, which will be discussed later)

 

We first encapsulate the previously defined Human class:


Public class Test
{
Public static void main (String [] args)
{
Human aPerson = new Human (160 );
System. out. println (aPerson. getHeight (); aPerson. growHeight (170); System. out. println (aPerson. getHeight (); aPerson. repeatBreath (100 );
}

}

Class Human
{
/**
* Constructor
*/
Public Human (int h)
{
This. height = h;
System. out. println ("I'm born ");
}

/**
* Accessor
*/
Public int getHeight ()
{
Return this. height;
}

/**
* Mutator
*/
Public void growHeight (int h)
{
This. height = this. height + h;
}

/**
* Encapsulated, for internal use
*/
Private void breath ()
{
System. out. println ("hu ...");
}


/**
* Call breath ()
*/
Public void repeatBreath (int rep)
{
Int I;
For (I = 0; I <rep; I ++ ){
This. breath ();
}
}

Private int height; // encapsulated, for internal use
}

Human can call any member internally, even if it is set to private height and breath ()

When we are outside Human, for example, Test, we can only call Members defined as public in Human, rather than those specified as private.

Through encapsulation, the Human class only retains the following methods as interfaces:

• GetHeight ()
• GrowHeight ()
• RepBreath ()
 

We can represent the Human class and its interfaces as follows:

 

"Remote Control with Shell added"

 

If we use a strong line from main to call height:

System. out. println (aPerson. height); the following error message is displayed:

Test. java: 6: height has private access in Human
System. out. println (aPerson. height );
^
1 error

Beep, you got an electric shock! A private member cannot be called externally.

 

In general Java specifications, data members that express states (such as height) must be set to private. Modify data members through methods provided by the interface (such as getHeight () and growHeight ()). This specification protects data. You cannot directly modify data. You must use the corresponding method to read and write data. Class designers can add data usage specifications to interface methods.

 

Class Encapsulation
In A. java file, only one class can contain the public keyword, such as the Test class above. Therefore, we can directly call this class from any other class. The Human class has no keywords. Earlier, the members of our object had no keywords. This absence of keywords also represents a kind of visibility. I will go deep into the package explanation.

 

Finally, we can try to encapsulate a Torch class to express our scheme. Interfaces can be switched and charged. Internal members can have power.

 

 

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.