Inherited
Inheritance makes it easier to implement class extensions. Using extends
Note:
- Classes in Java have only single inheritance, and interfaces have multiple inheritance.
- Subclasses inherit the parent class, and you can get all the properties and methods of the parent class (except for the parent class's constructor), but not necessarily directly accessible (for example, properties and methods that are private to the parent class).
- Parent class of all classes: Object.
- The instanceof is a two-tuple operator, the object on the left, the class on the right, and true if the object is the object created by the class or subclass that is on the right; otherwise, false.
Override of Method override
Subclasses can replace the behavior of the parent class with their own behavior by overriding the methods of the parent class. The rewriting of a method is a necessary condition for implementing polymorphism.
The rewrite of the method needs to meet the following three points:
- "= =": Method name, formal parameter list is the same.
- "≤": Returns the value type and declares the exception type, and the subclass is less than or equal to the parent class.
- "≥": access rights, subclasses greater than or equal to the parent class.
= = and Equals method
the "= =" represents the comparison between the two sides. If it is the base type, the value is equal, and if it is a reference type, the address is equal
The Equals method of object, by default, compares the hashcode of two objects, returns true if the reference is the same object, or false otherwise. However, we can rewrite the Equals method according to our own requirements.
Super
Super is a reference to the immediate parent class object. You can access the methods or properties covered by the quilt class in the parent class through Super.
If the first line of code for the constructor method does not explicitly call super (...) Or this (...); Java, by default, calls Super (), meaning that it calls the parameterless constructor of the parent class.
To construct the method invocation order:
Construction method The first sentence is always: super (...) To invoke the corresponding constructor method of the parent class. So, the process is to go back up to object, then execute the initialization block and construction method of the class in turn, until the current subclass.
Note: Static initialization of block invocation order, as in the order of constructor call, is no longer duplicated.
Packaging
Needs to let the user know to expose, does not need to let the user know all hides, this is the encapsulation. Encapsulation is the combination of the properties and operations of an object as a separate whole, as much as possible to hide the internal implementation details of the object.
Implementation of encapsulation--access control character
Private means that only their own classes can access
Default means no modifier adornments, only classes of the same package can access
Protected means that it can be accessed by the same package class and by subclasses in other packages
Public means that it can be accessed by all classes in all packages of the project
The handling of the properties of the class:
Private access is generally used.
Provides an appropriate Get/set method to access related properties, which are usually public-decorated to provide assignment and read operations to the property (note: The Get method of the Boolean variable is the start of the is!).
Some helper methods that are used only for this class can be decorated with private, and the methods that other classes invoke are intended to be decorated with public.
Polymorphic
Polymorphism refers to the same method call, which may behave differently due to different objects.
Note:
- Polymorphism is the polymorphism of the method, not the polymorphism of the attribute (polymorphism is independent of the attribute).
- There are 3 prerequisites for polymorphic existence: inheritance, method overrides, and parent-class references to child-class objects.
- When a parent class reference points to a subclass object, the parent class reference invokes the method overridden by the subclass, at which time the polymorphism appears.
Transformation of objects (casting)
The parent class reference points to the subclass object, which we call the process as an upward transformation, which belongs to the automatic type conversion.
An upward-transformed parent class reference variable can only call a method of its compiled type, and cannot invoke a method of its run-time type. At this point, we need to do a type of coercion, which we call downward transformation!
Final keyword
- Modifier variable: The variable that is modified by him cannot be changed. Once the initial value is assigned, it cannot be re-assigned.
- Modification method: The method cannot be overridden by a quilt class. But can be overloaded!
- Decorated class: A decorated class cannot be inherited. For example: Math, String, and so on.
Java three major features