Java basic 04 Package and Interface (reprint)

Source: Internet
Author: User

Data members and methods are both open to both internal and external. Inside the object, we use this to invoke the object's data members and methods. Objects can be used outside the object, such as when we call an object in another class. Data members and objects. Method () to invoke the object's data members and methods. We are going to encapsulate (encapsulation) The members of the object (members include data members and methods), thus allowing only the members of the part to be called from outside. 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 retains a limited number of external interfaces (interface), hiding specific implementation details. For example, in the Linux architecture, you can see that the Linux operating system encapsulates the specifics of the underlying hardware, leaving only the system calls to this set of interfaces. The user is outside the package, only through the interface, to do the required operations. Encapsulation is common in life. For example, the following a charging torch: a user even if you do not read the instructions, you can also guess the flashlight operation: switching and charging. The torch uses a plastic shell to hide the internal details that the user does not need to touch, leaving only two interfaces, 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, the user will feel overwhelmed with the product (such as a remote control without a shell). As a result, encapsulation improves the ease of use of the product. If the product is not encapsulated, many details of the flashlight or remote control will be exposed to the user: battery, circuit, sealed rubber and so on. While this allows the user to operate the product more freely, such as discharging the battery directly, removing an LED lamp, etc. However, users often have to bear the risk of greater damage to the product. As a result, the package improves product safety. A Java software PRODUCT is the same as a daily product. An object can have many members (data members and methods) inside it. There are some data members and methods that are only used internally. At this point, we would like to have a mechanism to "shell" the object, thus encapsulating 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 controls the external visibility of members of an object by three keywords (visibility): public, private, protected.

    • Public: The member is visible externally, that is, the member is part of the interface
    • Private: The member is not visible externally and can only be used for internal use and cannot be accessed externally.
    • Protected: Involving inheritance
Encapsulates 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;
}
private void Breath () {
System.out.println ("Hu.. Hu.. ");
}
public void Repeatbreath (int rep) {
int i;
for (i = 0; i<rep; i++) {
This.breath ();
}
}
private int height;
}

Internal methods are not affected by encapsulation. Human's internal method can call any member, even the height and breath () set to private

External methods can only invoke public members. When we are outside of human, such as test, we can only call members that are defined as public in human, and cannot invoke a member that is defined as private.

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

    • GetHeight ()
    • Growheight ()
    • Repbreath ()

If we forcibly call height from main:

System.out.println (Aperson.height);

There will be the following error message:

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

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

In the general specification of Java, data members that express state (such as height) are set to private. Modifications to the data members are made through the methods provided by the interface (such as GetHeight () and Growheight ()). This specification plays a role in protecting data. The user cannot modify the data directly, and the data must be read and written by the appropriate method. The designer of the class can include the usage specification of the data in the interface method.

Encapsulation of classes

In a. java file, there is only one class with the public keyword, such as the test class above. So, from any other class, we can call that class directly. The human class has no keywords. Earlier, the members of our object also had no keywords. This non-keyword scenario also represents a visibility that I'll dive into in the package.

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.