Java basic learning: final and polymorphism and type conversion; java basic final Polymorphism

Source: Internet
Author: User

Java basic learning: final and polymorphism and type conversion; java basic final Polymorphism

Final keywords:

The final keyword indicates the final meaning. It can modify classes, member variables, and member methods.

• Modifier class, which cannot be inherited

• When modifying a variable, the variable becomes a constant and can only be assigned once.

• Modification method. The method cannot be overwritten.


Final keyword interview questions

• Final modifies local variables

In the method, the variable cannot be changed, but can be assigned only once.

The method Declaration demonstrates the basic type and reference type as parameters respectively.

Basic type. The value cannot be changed.

The reference type, indicating that the address value cannot be changed. The value of the object in heap memory can be changed.

• Final variable initialization time

Before the object is constructed (non-static constants)

 

The permission modifier does not make any sense to modify local variables.

 

Polymorphism: the status of a thing at different times.

 

When you look at an object, you can see it from the left.

Example:

A cat can be a cat. Cat m = new CAT ();

At the same time, a cat is also a type of animal. It can also be called an animal.

Animal d = new CAT ();

 

Polymorphism premise and embodiment

• Inheritance relationships

• Method override

• A parent class references a subclass object.

 

Features of member access during Polymorphism

Member variables

Compile to the left, run to the left

Member Method

Compile to the left, run to the right

Static methods and Classes

Compile to the left, run to the left

Therefore, static methods cannot be used to calculate method rewriting.

 

Object Conversion Relationship

(1) upward Transformation: when creating an object, the object can be referenced as its own type, can be referenced as a parent type (assign a subclass object to a reference variable of the parent class ).

Upward transformation Syntax: <parent type> <reference variable> = new <child type>

The subclass method is actually called.

(2) downward Transformation: reference the parent class object as its subclass type (forced type conversion is required ).

Syntax for downward Transformation: <child type> <reference variable> = (<child type>) <parent type reference variable>;

Considerations for type conversion

(1) Up and down must both be inheritance relationships between two types.

(2) the system automatically converts the data type to the upper limit without force conversion.

(3) downward, only the referenced variables of the parent class can be converted to child classes. The instance objects of a parent class cannot be forcibly converted to child classes.

(4) The conversion of basic data types can only be numeric data (including integer, numeric, and floating point data), while the numeric data cannot be converted to the boolean data type.

 

Benefits of polymorphism: Improves the maintainability of code (embodied by inheritance) and enhances code scalability

 

 

 


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.