Each important definition in Java

Source: Internet
Author: User
Tags modifier

Inheriting subclasses can inherit all the methods and properties of the parent class (including "private", whether or not it is final), but subclasses cannot invoke the private methods and properties of the parent class directly. By reflection, subclasses can invoke private methods and properties of the parent class. Final

1. Data

The declaration data is a constant, either a compile-time or a constant that cannot be changed after the runtime is initialized.

    • For basic types, final causes the values to be unchanged;
    • For reference types, final makes the reference unchanged and cannot reference other objects, but the referenced object itself can be modified.
Final int x = 1= 2;  // cannot assign value to final variable ' x ' Final New  = 1;

2. Methods

Declaring a method cannot be overridden by a quilt class.

3. Class

Declaring a class is not allowed to be inherited.

Overloading (overload) and override (override)

Prerequisite for method rewriting: an inherited relationship must exist.

Method overrides: The child parent class has a function of the same name, which we call an override of the method.

What is the time to use the override of a method: When the functionality of the parent class does not meet the requirements of the subclass.

Override (Overwrite) the rule:

1. The parameter list of the overridden method must be exactly the same as the overridden method, otherwise it cannot be called overridden instead of overloaded.
2. The access modifier for the overridden method must be greater than the access modifier (public>protected>default>private) of the overridden method.
3, the overridden method, the subclass of the return value type must be less than or equal to the parent class return value type;
4. The overridden method throws an exception that must be the same as the exception thrown by the overridden method, or its subclass;
5. The overridden method cannot be private, otherwise only a new method is defined in its subclass (this can also be called overloading?). ), and it is not rewritten;
6, static methods can not be rewritten as a non-static method (compile error);
7. When the parent method is final, the subclass cannot override the method, regardless of whether the method is modified by public, protected, or by default.

Overloading of methods: there are two or more than two functions of the same name in a class, known as method overloads.
Rules for overloading:
1, in the use of overloading is generally only the same method name, different parameters of the form of implementation. Different parameter types can be different parameter types, numbers, or sequences;

There are exceptions that are distinguished by return values, but this method is not entirely possible, for example

void f () {}   int return 1 ; } // in int x =f (), it is true that overloaded methods can be distinguished, but it is not possible to use F () directly

So we only say that the method overload can only be implemented with the same method name and different parameter forms.

2, can not be overloaded by access rights, return type, thrown exception;
3. The exception type and number of methods do not affect overloading.

Super

Represents the corresponding object of the parent class, so super accesses the parent class members and methods that cannot be used directly in the subclass, not just the constructors that call the parent class

Polymorphic

Concept: Upward transformation and purpose, and downward transformation and purpose

Pre-binding (process-oriented only early binding) and late binding (dynamic binding, runtime binding)

The following are not polymorphic:

1. Subclass overrides private method of parent class

2. The subclass and the parent class have a member variable of the same name (the domain with the name), the access of a domain is at compile time, and any domain access operation will be resolved by the compiler, at this time the subclass and the parent class the same name of the domain is essentially two different storage space, So at this point, when the subclass object is transformed to a parent class reference is not polymorphic (actually rarely found because the members of the parent class are usually private and can only be accessed by invoking the method)

3. A parent class method is static, not polymorphic,

Constructors and polymorphism: The constructor is not polymorphic because it is actually a static method, and this declaration is implicitly

The order in which the code is executed in inheritance is: 1. Parent class static object, parent class static code block                                                2. Sub-category Static object, subclass static code block                                                  3. Parent class non-static object, parent non-static code block                                ,         &NB Sp      4. Parent class Constructors                                                  5. Subclass non-static object, childClass non-static code block                                                6. Subclass Constructors   Automatic unboxing

Source: Https://www.nowcoder.com/questionTerminal/643b145a860f457d8a150869e1a17eba
Automatic unpacking of the JDK is required on 1.5
1, basic type and basic type of package for the "= =" operator comparison, the basic package type will automatically remove the box into the basic type and then compare, so the integer (0) will automatically unboxing to the int type and then compare, obviously return true;
2, two Integer type "= =" comparison, if its value is 128 to 127, then return True, otherwise return false, which is related to integer.valueof () buffer object, of course, if this is a new object such as: Integer a= New Integer (1) and integer b=new integer (1), A==B returns false because the heap points to a different address value.

3, two basic type of package for equals () comparison, first equals () will compare the type, if the type is the same, continue to compare the value, if the value is the same, return True
4, the basic package type calls Equals (), but the parameter is the basic type, this time, the first automatic boxing, the basic type conversion to its package type, and then the comparison in 3.

int a=257; integer b=257; integer c=257; integer b2=57; integer c2=57; System.out.println (a==b); // true //  System.out.println (a.equals (b)); Compile error, basic type cannot call Equals ()System.out.println (B.equals (257.0)); // false, first 257.0 for encapsulation, then 3 for comparison System.out.println (B==C); // false System.out.println (B2==C2); // true

Each important definition in Java

Related Article

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.