Java Code Content Overview

Source: Internet
Author: User
Tags modifier

1. Overview and classification of code blocks
A: code block overview
In Java, code enclosed in {} is called a code block.
B: code block classification
Depending on its location and declaration, it can be divided into local code blocks, building blocks of code, static blocks of code, and synchronizing code blocks (multithreading).
C: Application of common code blocks
A: Local code block
To appear in a method; limit a variable's life cycle, release early, and improve memory utilization
B: Building blocks of code (initialization blocks)
Occurs outside of a method in a class, the same code is stored together in multiple constructor methods, each call construct executes, and is executed before the method is constructed
C: Static code block
Occurs outside of a method in a class, with a static modifier, to initialize the class, execute at load time, and execute only once.
Typically used to load the drive

2. Benefits and drawbacks of inheritance
A: Benefits of inheritance
A: Improved reusability of the code
B: Improved maintainability of the code
C: The relationship between classes and classes is a prerequisite for polymorphism
B: The drawbacks of inheritance
The coupling of the class is enhanced.

Principles of development: cohesion, low coupling.
Coupling: Class-to-class relationships
Cohesion: the ability to do something yourself

3. Inheritance characteristics of classes in Java
Inheritance characteristics of classes in A:java
A:java only supports single inheritance and does not support multiple inheritance. (a son can only have one father)
Some languages are supported for multiple inheritance, format: Extends Class 1, Class 2,...
B:java supports multilayer inheritance (inheritance system)

Inheritance considerations and when to use inheritance
A: Considerations for inheritance
A: Subclasses can only inherit all non-private members of the parent class (member methods and member variables)
B: Subclasses cannot inherit the constructor of the parent class, but can access the parent class construction method through the super (immediately speaking) keyword.
C: Do not inherit for part of the function

The difference and application of this and super
What do a:this and super mean?
This: Represents a reference to the current object, who calls me, I represent who
Super: A reference to the parent class of the current object
The difference between the use of B:this and super
A: Calling member variables
This member variable calls the member variable of this class, or it can call the member variable of the parent class
Super. member variables call member variables of the parent class
B: Call construction method
This (...) Calling this class's constructor method
Super (...) Calling the parent class's constructor method
C: Call member method
This member method calls the member method of this class, or it can call the method of the parent class
Super. Member methods call the parent class's Members method

4. The relationship between the construction methods in succession
A: Case Demo
All constructor methods in a subclass access the constructor of the parent's hollow parameter by default
B: Why?
Because subclasses inherit data from the parent class, data from the parent class may also be used.
Therefore, before subclasses are initialized, it is important to complete the initialization of the parent class data first.
The first statement of each constructor method defaults to the top-most parent class of the super () object class.

Considerations for constructing methods in inheritance

B: Precautions
Super (...) Or this (...) must appear on the first statement of the constructor method

Member method relationships in inheritance

Overview of method overrides and their applications
A: What is method rewriting
Rewrite: The child parent class appears exactly the same way (note: The return value type can be a child parent class, this we learned the object-oriented talk)
B: Application of Method rewriting:
You can override a method in a parent class when the subclass needs the functionality of the parent class, and the feature body subclass has its own unique content. In this way, the function of the parent class is inherited, and the content specific to the subclass is defined.

Considerations for method overrides
A: Method Rewriting considerations
A: Private methods in the parent class cannot be overridden
Because the parent class private method subclass simply cannot inherit
B: When subclasses override parent class methods, access permissions cannot be lower
It's best to be consistent
C: Parent class static methods, subclasses must also be overridden by static methods
In fact, this is not the method of rewriting, but the phenomenon is true, as to why not the method rewrite, polymorphic I will explain (static only cover static)
When a subclass overrides a parent class method, it is best to declare exactly the same.

method to rewrite the face question
A: Method rewrite the face question
What is the difference between override and overload? Can overload change the return value type?
Overload can change the return value type, just look at the parameter list
Method overrides: A method that is identical to the method declaration in the parent class appears in the subclass. With respect to the return value type, the return value is a consistent (or child-parent)
Method Overloading: The method names that appear in this class are the same as the parameter lists. Is independent of the return value type.

When a subclass object invokes a method:
Look for the subclass itself, and then look for the parent class.

5. Final modification Features
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 keyword decoration local variable
A: Case Demo
Method inside or on a method declaration (learn)

Basic type, value cannot be changed
Reference type, which is the address value cannot be changed, the property in the object can be changed
B:final the initialization time of a modified variable
Display initialization
Before object construction is complete

Java Code Content Overview

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.