1. Inheritance (inheritance)
Inheritance is the technique of building a new class using the definition of an existing class, and the definition of a new class can add new data or new functionality, or it can use the functionality of the parent class, but not selectively inherit the parent class. This technique makes it easy to reuse the previous code, greatly shortening the development cycle and reducing development costs.
Inheritance is intended to reuse the parent class code, while preparing for the implementation of polymorphism.
2, we can divide the class in Java into the following three kinds:
Class: A class that is defined with class and does not contain abstract methods.
Abstract class: A class defined using the abstract class, which can or may not contain abstract methods.
Interface: A class defined using interface.
3. There are the following inheritance rules between the three types:
Classes can inherit (extends) classes, which can inherit (extends) abstract classes, which can inherit (implements) interfaces.
Abstract classes can inherit (extends) classes, which can inherit (extends) abstract classes, which can inherit (implements) interfaces.
Interfaces can only inherit (extends) interfaces.
4. Package Benefits
1: Improved reusability of code. 2: The relationship between classes and classes provides a precondition for another feature polymorphism.The origin of the parent class: In fact, more than one class is constantly extracting the common content. Javain the case of inheritance, Java only supports single inheritance. Although Java does not directly support multiple inheritance, it retains this multi-inheritance mechanism for improvement. Why not support multiple inheritance? Because when a class inherits two parent classes, the same functionality is available in two parent classes, which one does the subclass object run when it calls the function? Because the method body exists in the method in the parent class. But Java supports multiple inheritance. A inherits b b inherits C C inherits D. The emergence of multiple inheritance, there is an inheritance system. The top-level parent class in the system is drawn from the constant upward. It defines the function of the most basic and common content of the system. Therefore, a system to be used, direct access to the function of the parent class in the system to know the basic use of the system. When you want to use a system, you need to create objects. It is recommended that you establish the most child class object, because the most subclasses can not only use the functionality in the parent class. You can also use some of the features that are specific to subclasses. Simply put: For the use of an inheritance system, consult the contents of the top-level parent class to create the object of the bottommost subclass. 5. Relationship of subclass to parent Class 1: member variable. when the same property appears in the child parent class, the object of the subclass type is called, and the value is the property value of the child class. if you want to invoke a property value in the parent class, you need to use a keyword: Super This:represents an object reference of this class type. Super:represents a memory space reference in the parent class to which the child class belongs. Note: There is usually no member variable of the same name in the child parent class, because as long as the parent class is defined, the subclass is not defined and inherited directly. 2: member function. When the same method appears in the child parent class, creating a subclass object runs the method in the subclass. As if the methods in the parent class were overwritten. So this is another feature of the function: Overwrite (duplicate, rewrite) when to use overwrite? When the functional content of a class needs to be modified, it can be implemented by overwriting. 3: constructor. When the subclass constructor is found to run, the constructor for the parent class is run first. Why? Cause: The first row in all the constructors of a subclass has a stealth statement super (); Super (): Represents the constructor of the parent class, and it is applied to the constructor in the parent class that corresponds to the parameter. Super (): is the constructor that calls the parent-class hollow argument. Why do I need to call a function in the parent class when the subclass object is initialized? (Why join the Super () in the first line of the subclass constructor)?) Because subclasses inherit the parent class, they inherit the data from the parent class, so it is important to see how the parent class initializes its own data. So when the subclass initializes the object, it calls the constructor of the parent class, which is the instantiation of the subclass. Note: All constructors in a subclass access the constructor of the null argument in the parent class by default, because the first row within each subclass construct has the default statement super (), and if there is no constructor for the null argument in the parent class, within the constructor of the subclass, You must specify the constructors in the parent class to access through the super statement. If the subclass constructor uses this to specify the calling subclass's own constructor, the constructor that is called will also access the constructor in the parent class.question: Super () and this () can appear in the constructor at the same time. Two statements can only have one definition in the first row, so only one can appear.Super ()or This (): Why must I define it in the first line? Because super () or this () is called the constructor, the constructor is used for initialization, so the initialization action is done first. 6, when to use inheritance when there is a relationship between the class and the class, the premise of inheritance. A is one of the B's. A inherits B. Wolves are one of the canine branches. Note: Do not inherit just to get an existing member in another class. So to judge the relationship, you can simply see, if inherited, the function of the inherited class, can be the subclass has, then the inheritance is established. If not, you cannot inherit.8, method coverage, pay attention to two points:1: When a subclass overrides a parent class, it must be guaranteed that the permissions of the subclass method must be greater than or equal to the parent class method permission to implement inheritance. Otherwise, the compilation fails. 2: Overwrite, either static, or not static. (Static can only be overridden by static, or statically overwritten) 9,one drawback of inheritance:Break the encapsulation. For some classes, or functions in a class, they need to be inherited, or replicated.
Java object-oriented-inheritance