Java Learning Note day05

Source: Internet
Author: User

1.final keywords: Prevent inherited classes or overwrite method modifications, variables or methods are final defined
Will exist in the inner

Characteristics:
1) You can modify classes, functions, and variables.
2) A class that is final modified cannot be inherited.
3) The final modified method cannot be overridden.
4) The final modified variable is a constant and can only be assigned once. The variable that is usually decorated is capitalized in all letters.
5) The inner class can only access the final modified local variables, which is less used.

2. Abstract class:
The class in which the abstract method resides must be marked as an abstract class, so that abstract classes exist because of abstract methods.
Characteristics:
1) Abstract classes cannot create objects with new ().


3. Inheriting classes
When a subclass and parent class have the same method, the subclass inherits the parent class, and the child class object calls the method by default.
Call the method in the subclass, and if you need to call the method in the parent class, the first sentence within the method of the subclass is super. Method ();
Note: When a subclass method overrides a parent class method, the adornment permission for the method must be greater than or equal to the parent class.
A static method in a subclass can only overwrite static methods in the parent class.

4. Polymorphism: The appearance of polymorphism is to improve the extensibility of the program
Thought: Through the object-oriented thought, can direct objects to do things, if the object more, command every object, it becomes troublesome.

How to simplify the troublesome things? After analyzing these objects and extracting them upwards, common types can be commanded.

Do things.

Three conditions for polymorphism: inheritance, override, parent class reference to child class object.
Description: 1) There must be a relationship between class and class
2) Overwrite action is usually present

  on the program:
      1) The parent class or interface reference points to its own subclass object
          such as: Father f = new Son (); //fater can be a class or an interface
   
       2) The parent class or interface reference receives its own subclass object
         such as: There is a method passed with a parameter of parent class or subclass
              method (Father f) {
                 ...
            }
         When the change method is called, the
              method (New Son) {
                 ...
            }

The characteristics of member functions in polymorphism:
1) Compile time, refer to whether the class string to which the reference variable belongs has a method called
2) runtime, refer to whether the object belongs to a class that has a method called
That is: Look to the left at compile time and run to the right.

Polymorphic problems:
1) When the parent class refers to an object whose subclass is F, it cannot access members that belong to the subclass object through F.
Why can't you do that? LZ have not thought, because F is fatherclass, so the compiler only know F has fatherclass.class

Information, information other than Fatherclass.class, the compiler does not know, and the object members of the subclass are in Sonclass.class, which is

Say outside of Fatherclass.class, so f cannot access the object members of subclasses

2) If there is an override of the parent class method in the subclass, then according to the polymorphic mechanism, the actual access is the subclass when the method is accessed through F

The method that is overridden in the.
Why is this possible? It says that F can only access fatherclass.class information (note that this refers to the compile-time compiler only knows F

Is the Fatherclass type, does not know what object F specifically points to, the runtime knows what to point to, and the subclass overrides the method, the parent

Class also exists, that is, the method of Sonclass.class rewriting, Fatherclass.class also (if there is sonclass.class in the

Fatherclass.class does not have a method, F also cannot be called directly), so f can be accessed, but when called (note here refers to the

Is the run-time), F is actually pointing to the Sonclass object, so the method called is the Sonclass object.

3) The problem is that if a subclass is overridden by a method that accesses a member variable that belongs exclusively to a subclass, then the parent class reference F can also call that

Is the method rewritten?
Yes, to distinguish between the compilation period and the runtime, compile time is the compiler check syntax and type, runtime is the parser to parse pseudo-code for machine instructions

Instead, the compile-time compiler checks the access range of F, which means that f accesses no more than Fatherclass.class information without error.

The run-time parser parses the code directives of the method, because F points to the subclass object, so it resolves the method code directives that the subclass overrides, and the subclass

The memory space of an object is the space of the member variable that contains the child class, so there is no problem that the subclass member variable does not have memory allocated, so

can be called.

Java Learning Note day05

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.