- Inheritance: Inheritance is the most noticeable feature of object-oriented. Inheritance is the derivation of new classes from existing classes, which can absorb data properties and behaviors of existing classes, and can extend new capabilities.
Improved reusability of code. There is a relationship between classes and classes, and with this relationship, there is a polymorphic nature. In the Java language, Java only supports single inheritance and does not support multiple inheritance, because multiple inheritance is prone to security implications. But Java retains this mechanism, called multi-implementation. (If multiple inheritance is supported, when the same function is defined in more than one parent class, but its contents are different, the subclass calls this method without knowing which method in the parent class is called)
Note: Never simplify the code to get the functionality of other classes to inherit. You must have an owning relationship between the class and the class to inherit.
- Java supports multiple layers of inheritance, which is an inheritance system. When we want to use a system, we first look at the description of the parent class, because the parent class defines the common functionality in the system. By understanding the common function, we can know the basic function of the system, then the system can be used basically. In the case of a specific invocation, create the object of the most child class, because it is possible that the parent class cannot create objects (for example, abstract classes). There is also the ability to create subclasses that can use more functionality, including basic and also unique.
Summary: Consult the parent class feature to create a subclass object usage feature.
- If a non-private member variable with the same name appears in the subclass, the subclass accesses the variable in this class, with this, to access the variable in the parent class, with super.
The use of super is basically consistent with this. The former represents a reference to the parent class object, which represents a reference to the child class object.
- When a child class has a function that is exactly the same as the parent class, when the subclass object calls the function, the contents of the subclass function are run. Just as the function of the parent class is overwritten. This is called a function rewrite.
When the subclass inherits the parent class, the parent class is inherited from the function into the subclass. However, although the subclass has this function, the content of the function is inconsistent with the parent class. At this point, it is not necessary to define new features, but instead to use overrides, preserve the functionality definitions of the parent class, and override the feature content.
Note: Subclasses override the parent class, and you must ensure that the subclass permissions are not lower than the parent class permissions before they can be overwritten, or the compilation fails.
Static can only be overridden by static.
- Constructor in child parent class: The constructor of the parent class is also run when the child class object is initialized. That's because the constructor of the subclass defaults to the first line with one line of implicit code super (). This code accesses the constructor of the parent's hollow parameter, and all constructors in the subclass will have this line of code by default on the first line.
- Why subclasses must access constructors in the parent class: Because the data subclasses in the parent class can be fetched directly, the subclass object needs to see how the parent class initializes the data when it is created, so the subclass object accesses the constructor in the parent class when it initializes the data. If you want to specify a constructor method that accesses the parent class, you can specify it with the super () display.
NOTE: The super statement must be defined in the first row of the subclass constructor.
Conclusion: All constructors of subclasses have access to the constructor of the parent's hollow argument by default, because the first row within each constructor of the subclass has an implicit super (); When the constructor of the parent class does not have an empty argument, the child class must display the constructor in the parent class in the form of a super statement that specifies access.
- Final keyword: end. is a modifier. Classes, functions, and variables can be decorated. A final modified class cannot be inherited (in order to avoid being inherited after the quilt-like replication function) is final modified method, cannot be replicated. The final modified variable is a constant, can only be assigned once, the member variables and local variables can be decorated (when describing things, some data when the value of fixed, then in order to increase the readability of the code, give these values a name, to facilitate reading, and this value does not need to change, So add the final modifier)
- When an inner class is defined at a local location in a class, it can only access local variables that are partially final decorated.
- Abstract class: Abstraction can also be used to modify the method, the abstract method must be in the abstract class (if there is an abstract method in a class that is not an abstract class, when this class produces an object and calls this abstract method, there is no reasonable result), Both abstract methods and abstract classes must be decorated with the abstract keyword. You cannot create an object with new in an abstract class, because invoking an abstract method is meaningless. To be used in an abstract class, a subclass object invocation must be established after all the abstract methods have been replicated by the subclass. If a subclass overrides only a subset of the abstract methods, then the subclass is an abstract class.
- Template Method Design pattern: When a feature is defined, part of the function is deterministic, but some are indeterminate, and the identified part is used in the indeterminate part. Then the indeterminate part is exposed and the subclass of the class is completed.
- Interface (interface): is the declaration of a series of methods, is a collection of methods features, an interface only the methods of the characteristics of the method is not implemented, so these methods can be implemented in different classes, and these implementations can have different behavior (function). Constants in the interface are generally decorated with public static final, and the method is modified with public abstract. (members in the interface are public)
interfaces, like abstract classes, cannot be instantiated, because they have an abstract method in which the interface requires a Quilt class implementation (implements) and subclasses rewrite all the abstract methods in the interface to be instantiated.
- Interfaces can be implemented by multiple classes, that is, a form of conversion that does not support multiple inheritance, and is implemented more. Because a method in an interface has no method body, and all have the same method in multiple interfaces implemented by a class, the implementation class only needs to rewrite the method, which overrides the abstract method in all the interfaces implemented. Interfaces can also inherit interfaces, but there are multiple inheritance between interfaces, and the same reason is that all methods have no principals.
- A class can implement multiple interfaces while inheriting a class.
Summary: A. An interface is a rule that is exposed externally. B. An interface is a function extension of a program. C. Interfaces can be used for multiple implementations.
D A class is an implementation relationship between interfaces, and a class can inherit a class and implement multiple interfaces at the same time.
E. There can be an inheritance relationship between an interface and an interface, or a multiple inheritance relationship.
- The main function of implementing an interface is to extend functionality to the class, inheriting abstract classes that use common characteristics and behaviors
Beginners To learn Java, is javase, every day I learn from the day and notes, I hope you can criticize and advice, and promote common progress! Seventh Day!