The encapsulation and interface of the Java Basics Tutorial _java

Source: Internet
Author: User
Tags touch visibility

To sum up the previous content, objects (object) refer to a Thing, Class (class) refers to the type of the image. objects can have states and actions, that is, data members and methods.

Until now, data members and methods have been open to both internal and external. Inside the object, we use this to invoke the object's data members and methods. Outside the object, such as when we call an object in another class, we can use the object. Data members and objects. Method () to invoke the data members and methods of the object.

We are going to encapsulate (encapsulation) The members of the object (members include data members and methods), allowing only members from the external invocation part. With encapsulation, we can improve the ease of use and security of objects.

Encapsulation and interface

Encapsulation (encapsulation) is a common term for computers, that is, to keep a limited external interface (interface) and hide specific implementation details. For example, in the Linux architecture, you can see that the Linux operating system encapsulates the details of the underlying hardware, leaving only the set of interfaces that the system calls. The user is on the outside of the package and can only perform the desired operation through the interface.

Encapsulation is common in life. For example, here is a rechargeable flashlight:

A user can guess the operation of the torch, even without looking at the instructions: switch and charge. The torch uses a plastic shell to hide the internal details that the user does not need to touch, retaining only two connectors, switches, and electrical plugs. With these two interfaces, the user is sufficient to use the functionality that the product wants to implement in the design. If all the details are exposed to the user at the same time, users will be overwhelmed by the product (for example, a remote control with no shell below). Therefore, encapsulation improves the usability of the product.

If the product is not packaged, many details of the torch or remote control will be exposed to the user: battery, circuit, sealed rubber and so on. Although this allows the user to be more free to operate on the product, such as directly to the battery discharge, take out an LED lamp and so on. However, users often have to bear the risk of greater damage to the product. Therefore, encapsulation improves the security of the product.

A Java software PRODUCT is the same as a daily product. There can be many members (data members and methods) within an object. Some data members and methods are only used internally. At this point, we would like to have an object "shell" mechanism to encapsulate the object. This makes it easier for users to learn and use external interfaces without having to touch internal members.

Encapsulation of Object Members

Java uses three keywords to control the external visibility of members of an object (visibility): public, private, protected.

1.public: The member is visible outside, that is, the member is part of the interface
2.private: This member is not visible outside, can only be used for internal use and cannot be accessed from outside.
(protected refers to the concept of inheritance, put it in later)

Let's first encapsulate the previously defined human class:

Copy Code code as follows:

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...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
}

Internal methods are not affected by encapsulation. The internal method of human can call any member, even if it is set to private height and breath ()

External methods can only invoke public members. When we are outside the human, such as test, we can only invoke members that are specified as public in human, not members that are specified as private.

By encapsulation, the human class retains only the following methods as an interface:

1.getHeight ()
2.growHeight ()
3.repBreath ()

We can represent the human class and its interfaces in the form of the following diagram:


"The shell-plus remote."

If we forcibly call height from main:

Copy Code code as follows:

System.out.println (Aperson.height);

You will be prompted with the following error:
Copy Code code as follows:

Test.java:6: Height has private access in Human
System.out.println (Aperson.height);
^
1 Error

Beep, you got electrocuted! A member that is described as private and cannot be called externally.

In Java's usual specification, the data member that expresses the state, such as height, is set to private. Modifications to data members are done through the methods provided by the interface (such as GetHeight () and Growheight ()). This specification plays a role in protecting data. Users cannot modify data directly, and must be able to read and write data in the appropriate way. The designer of the class can add the usage specification of the data to the interface method.

Encapsulation of a class

In a. java file, there is only one class with a public keyword, such as the test class above. So, from any other class, we can call the class directly. The human class has no keywords. Earlier, the members of our object did not have a keyword. This absence of keywords also represents a visibility that I will delve into in the package of the package.

Practice encapsulates a torch class to represent a flashlight. The interface has a switch and a charge. The members of the interior have electricity.

Summarize

Encapsulation, interface
Private, public

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.