Final and polymorphic and type conversion of basic Java Learning

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.