Final keyword:
The final keyword is the ultimate meaning that can be decorated with classes, member variables, and member methods.
? Modifier class, class cannot be inherited
? Modifier variable, the variable becomes a constant and can only be assigned once.
? Modification method, method cannot be overridden
Final key literal question
? Final modifier local variable
Within a method, the variable cannot be changed and can be assigned only once.
On the method declaration, the case of the basic type and the reference type as arguments are shown separately
Basic type, value cannot be changed
Reference type, which is the address value cannot be changed. The value of the object in the heap memory can be changed.
? Initialization time for final modified variables
Before object construction is complete (non-static constant)
The permission modifier modifies the local variable without any meaning.
Polymorphism: The different states of a thing that appear at different times.
Look at the object, from there to the left.
Example:
A cat can be a type of cat. Cat m = new Cat ();
The cat is also a kind of animal, can also call the cat animal.
Animal d = new Cat ();
Multi-state premise and embodiment
? Have an inheritance relationship
? There are methods to rewrite
? There are parent class references to child class objects
feature of member access at polymorphic time
Member variables
Compile look left, run look left
Member Methods
Compile look left, run look right
Static methods and class-related
Compile look left, run look left
So static methods cannot be overridden by methods
Transformation relationships for objects
(1) Upward transformation: When an object is created, the object can be referred to as its own type, and can be a reference to the parent type (assigning a subclass object directly to a reference variable of a parent class).
Transition up Syntax:< parent type >< reference variable >=new< subtype >
The method that is actually called is the child class.
(2) Down transformation: a reference to the parent class's object as a type of its child class (requires coercion of type conversions).
Downward transition Syntax:< subtype > < reference variable > = (< subtype >) < parent type reference variable >;
Type conversion Considerations
(1) Up, down must be an inheritance relationship between two types.
(2) When up, the system automatically converts the type without casting.
(3) down, only the reference variable of the parent class can be converted to a subclass, and an instance object of a parent class cannot be cast to a subclass.
(4) The conversion of the basic data type can only be numeric data (including integer, character, floating-point type), and the numeric type cannot be converted with the Boolean type.
The benefits of polymorphism: Improved code maintainability (embodied by inheritance) increases the extensibility of the Code
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Final and polymorphic and type conversion of basic Java Learning