Hello, ladies and gentlemen, last time we talked about object-oriented in Java, this time we will talk about encapsulation in Java. Stop gossiping and return to the truth. Let’s Talk Android!
See, classes are provided in Java to encapsulate variables and functions of various types. Next, we’ll use pseudocode to demonstrate the class:
class ClassName
{
permission type ValueName;
permission function;
}
Copy the code
Class in the code is the Java keyword that represents the type of class, and ClassName is the name of the type. The content in parentheses is the content wrapped by the class. These can be divided into two parts:
-
Some are variables of various types, which we call member variables (or attributes) of the class.Copy the code
-
Some are functions, which we call member functions (or methods) of the class.Copy the code
They differ from ordinary variables and functions in that there are three permissions in front of the type:
-
public Copy the code
-
protected Copy the code
-
private Copy the code
They are used to restrict access to members. The access permissions of the three are in descending order.
-
Public permissions allow access to members of a class both inside and outside the class.Copy the code
-
Protected permissions allow access to members of a class inside the class and in subclasses that inherit from the class.Copy the code
-
Private permissions allow access to members only within a class;Copy the code
There is one special function that needs to be explained: constructors. What’s the special method? It returns no value, and the function name is the same as the class name. This function is mainly used to initialize member variables. It can also be overloaded. We can define multiple overloaded functions to perform different initialization operations. This function is called automatically when an object of the class is created, but we can also call it manually: this(). This means calling the constructor of the current class.
In addition to encapsulating variables and functions, classes can also encapsulate classes. For example, ClassA encapsulates ClassB internally, so we call ClassB ClassA’s inner class. The definition of an inner class is the same as that of a normal class, which we won’t go into in detail. Class provides an abstract type. If we want to use this type, we need to instantiate the class, that is, create variables. Next, we use pseudocode to demonstrate how to create variables of this type:
ClassName clsVal = new ClassName();
Copy the code
In this case, the new keyword is the Java keyword, which is used to create a memory space in the heap for objects of class type. However, the memory reclamation work does not need to be manually done. The virtual machine will automatically reclaim the memory. In addition, the constructor of the class is automatically called to do some initialization for the object.
An object of a class has all the member variables of the class, but the values of the member variables vary from object to object. If you want all objects of a class to have the same value of a member variable, you define such a variable as static. How do you define it? When defining a member variable, add the static keyword after the access permission. Such member variables belong to the class itself, not the object of the class. This also has the advantage that the objects of the class do not need to allocate memory for such member variables, thus saving memory space. In addition, member functions can also be defined as static, defining methods and member variables identically. We said that a class can only be used if it is instantiated as an object, but there is one class that cannot instantiate an object. What class? An abstract class.
abstract class ClassName
{
// other thing
};
Copy the code
Such classes are abstract classes that contain abstract member functions (which use abstract in front of normal member functions and have no concrete operations). What’s the use of this class when you can’t instantiate an object? We only need to implement abstract functions in subclasses that inherit from the class. Once the abstract function is implemented, the subclass is no longer abstract and can instantiate objects just like a normal class.
That’s it for Java encapsulation. If you want to know more examples, listen next time.