Learn more about the advanced knowledge of object-oriented programming, related to inheritance in Java today.
An important feature of object-oriented programming is the reuse of classes, which can be implemented by two methods, one is to treat an instance of one class as a property of another class, and the other is to use the inheritance of the class, and the keyword extends allows one class to inherit another class. The basic content of inheritance is the same as C + +, but in Java it is important to note that: 1. A class can only inherit from one parent class and cannot inherit from more than one class, that is, "single-Inheritance" mode ; 2. There is an object class in the Java.lang package, which is the top-level parent class for all classes, and all Java classes, including classes in the standard library and classes of their own, inherit this class directly or indirectly. This class does not have any properties, just defines some methods, so in Java, as long as you define a Java class, there are some default methods for you to invoke.
About access control issues. In Java, you can control some access to classes by adding modifiers to the class, the properties of the class, and the methods of the class. In Java, 3 modifiers are defined to control the scope of access to classes, properties of classes, and methods of classes. Private: The limit is the most restrictive, the use of this keyword to restrict the properties or methods can only be accessed in the same class, it should be noted that this modifier can not be used in front of the class; default: in Java, default is not a keyword . Just a salutation to the class, the properties of the class, and the access to the methods of the class. If the class, the property of the class, does not add any modifiers before the method, the access permission is default, in which case only the class itself or other classes in the same package can access the properties or methods that are not accessible to the classes in the other package; protected: Properties or methods decorated with this modifier can be accessed by the same class, classes and subclasses in the same package, and it is important to note that this modifier is also not used in front of the class ; public: This modifier is available for classes, properties of classes, and methods of classes, and is the most lenient restriction Class properties that are decorated with this modifier can be accessed by any other class. In general, a property or method that is unrelated to other classes should be set to private only if it is set to public or protected by a property or method to which it is to be accessed by another class, or without any modifiers, that is, default.
Super keyword: In the process of inheriting a parent class from a subclass, you might need to call a member of the parent class in a subclass, such as a property, method, or constructor, which is done using the Super keyword.
1. Call the constructor of the parent class: Super (Parameter required by the parent class constructor)
2. Calling the parent class Property: Super. Properties
3. Calling the Parent class method: Super. Method ()
It is important to note that when you invoke a property or method of a parent class using the Super keyword, the properties or methods of the parent class must be those of the protected or public, and so that the child class can access the property or method. Super is primarily used to define a property in a subclass that has the same name as the parent class, or to overwrite the method, but also to access the property of the same name or the previous method in the parent class in the child class.
This keyword: When writing a method of a class, you want to get a reference to the current object, and Java introduces the keyword this, This represents the current object of the method in which it is located: 1. The constructor refers to the new object created by the constructor, 2. The object that invokes the method in the method, and 3. The instance variable or method of the class is referenced in the method or constructor of the class itself.
For the property content of the current object using this call the same as C + +, that is, the authority variable name and domain name, use this to distinguish, and the other must use the This keyword is the case, you need to explicitly indicate in the object that the current object reference is this object, For example, when you need to return the current object, and the constructor is called in the constructor, in a class, because of different initialization conditions, it is possible to define multiple constructors, in which a piece of code in one constructor is exactly the same as another. Then you can call the other constructor directly in this constructor, using the This keyword, the syntax is as follows: this (parameter), the system will be based on the number and type of parameters to find the matching constructor in the class. It is important to note that only one constructor can be called at most once in a constructor, and the call action must be at the beginning of the constructor.
Inheritance in Java and other related content