The concept of encapsulation mechanism encapsulation

Source: Internet
Author: User
Tags abstract class definition inheritance modifier modifiers
4.2
Seal
Loaded
Machine
System
4.2.1
The concept of encapsulation
Encapsulation, also known as information hiding, refers to the use of abstract data types to put data and based on
The operation of the data is encapsulated together so that it forms an indivisible independent entity, the number
It is protected in the abstract data type of the interior, as far as possible to hide internal details, only
Keep some external interfaces to make them contact with the outside. Other parts of the system are only pass
An authorized operation that has been wrapped outside the data to communicate with this abstract data type
Interaction. That is, the user does not need to know the implementation details of the internal method of the object, but
To access the object based on the external interface (object name and parameters) provided by the object. Packaging tools
Has the following characteristics:
4th Chapter Class and Object
(1) Setting Access object properties (data members) and methods in the definition of a class (
Member method, which restricts the scope of objects used by objects of this class and other classes.
(2) Provide an interface to describe how other objects are used.
(3) Other objects cannot directly modify the properties and methods owned by this object.
4th Chapter Class and Object
Encapsulation reflects the relative independence of things. The role of encapsulation in programming is to make
Objects outside of the object are not allowed to access the object's internal data (properties), thereby effectively
To avoid the "cross infection" of external errors. On the other hand, when the inside of an object
The ministry made some modifications, since it provided services only through a small number of interfaces,
Thus greatly reducing the internal impact of the changes on the external.
The package unit of object-oriented System is object, the class concept itself also has the encapsulation
Because the attributes of an object are described by the class description to which it belongs.
4th Chapter Class and Object
4.2.2
Rigorous definition of a class
In section 4.1.5, we have given the general format of the definition class, when I
The structure of the class given is:
Class name
{data member
Member Methods
}
4th Chapter Class and Object
This structure definition gives only what is necessary to define a class, and ignores
Many details of the class definition. With the concept of encapsulation, we can further
To learn the rigorous definition of a class. The rigorous definition format for the class is as follows:
[
Class modifier] Class
Class name
[Extends
Parent class name] [implements
Interface columns
Table
{
Data members
Member Methods
}
4th Chapter Class and Object
As you can see, in the rigorous definition format of a class, the description section of the class increases the
[Class modifier], [Extends parent class name], and [Implements interface list] three optional
Item With these options in place, you can fully demonstrate encapsulation, inheritance, and
Object-oriented features such as information hiding. Because of the complexity of this part of the content, we
A brief explanation is presented here, and will be discussed in detail in subsequent chapters.
Class modifiers (qualifier): Used to specify some of the peculiarities of a class, mainly
Describes the access restrictions on it.
4th Chapter Class and Object
Extends parent class Name: Indicates that the newly defined class is derived from a parent class that already exists
Out of it. In this way, the newly defined class can inherit an existing class--the parent
Some characteristics of the class.
Implements interface list: Java originally only supports single inheritance, in order to give
Multi-inheritance software development provides the convenience, it provides this interface mechanism.
4th Chapter Class and Object
4.2.3
Class modifiers
The modifier of a class is used to illustrate access restrictions on it, and a class can not fix
You can also have several different modifiers, public, final, abstract, and so on.
Their role is different, the following are described separately.
1
. No modifiers
If a class has no modifiers before it, the class can only be used by the class in the same package
Use. Java stipulates that all classes in the same program file are in the same package.
This means that a class without modifiers can be made by a class in the same program file
, but cannot be used by classes in other packages in other program files.
4th Chapter Class and Object
"Sample program C4_4.java"
Class PP//No modifiers
{int a=45;//PP class's data member A
}
public class C4_4//C4_4
{
public static void Main (string[] args)
{pp p1=new pp ()///class C4_4 creates an object with no Modifier-class pp P1
System.out.println (p1.a);
}
}
4th Chapter Class and Object
Two classes are defined in this program: Class PP and public class without modifiers
C4_4. They are two classes in the same program file (that is, the same package)
In the class C4_4, you can create an object P1 for the PP class, and the object P1 can reference the class
data member A of pp. The access restrictions for data members are discussed in section 4.3.
4th Chapter Class and Object
2
. Public
Modifiers
If the modifier for a class is public
, the class is a public class.
<

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.