Java Series Learning (vii)-Inheritance

Source: Internet
Author: User

1. Code block

(1) In Java, code enclosed in {} is called a code block.

(2) Classification:

A: Local code block

"Local Location"

"Function: Used to define the life cycle of a variable"

B: Building Blocks of code

"In the class, in the position of the member, the code enclosed in {}. The construction code block is executed before each call to the construction method execution. 】

"Function: You can put the common code of multiple construction methods together, initialize the object"

C: Static code block

"In the member position of the class, the code enclosed in {}, except that it is decorated with static"

"Function: Typically Initializes a class"

(3) Conclusion:

A: Order of execution:

Static code block > construct code block > Construct method

B: Static code block: Execute only once

C: Construct code block: Every call to construct method executes

2. Inheritance

(1) to extract the same members from multiple classes into a separate class, and then have multiple classes and that independent class produce a relationship.

This relationship is called inheritance.

(2) Inheritance is represented in Java

A: Use keyword extends to express

B: Format

(3) Benefits of inheritance:

A: Improved reusability of the code

B: Improved maintainability of the code

C: Let the class and the class have a relationship, is the precondition of polymorphism

(4) Disadvantages of inheritance:

A: To enhance the coupling of the class. Changes to this class can affect other classes related to that class.

"Principle: low coupling, cohesion poly"

"Coupling: Class-To-Class relationships"

"Cohesion: the ability to do something yourself"

B: Breaking the sealing

(5) Characteristics of inheritance in Java:

Class in A:java only supports single inheritance

B:java can be inherited in multiple layers

(6) Considerations of succession

A: Subclasses cannot inherit private members of parent class

B: Subclasses cannot inherit the constructor of a parent class, but can access it through super

C: Do not inherit for part of the function

(7) When to use inheritance

A: Inheritance embodies: is a relationship (Apple is a fruit)

B: Using the Assumption method

(8) Member Relationships in Java inheritance

A: Member variables

"The member variable name of the child class is not the same as the member variable name of the parent class"

"The member variable name of the child class is the same as the member variable name of the parent class"

"The specified member variable in the parent class is called in the child class"

B: Construction method

"The constructor method of a subclass accesses the parameterless constructor of the parent class by default."

"If there is no parameterless construction method in the subclass"

Method One: Add a constructor with parameters to the parent class

Method Two: Using the Super keyword to display the call to the parent class with a parameter construction method

Method Three: Subclasses use this to call other constructor methods of this class

"There must be a constructor in the subclass to access the parent class, otherwise the parent data is not initialized"

"Note" This (...) and super (...) must be placed in the first statement, otherwise the parent class will be initialized multiple times

C: Member Method

"The member method name of the child class is different from the member method name of the parent class"

"The member method name of the child class is the same as the member method name of the parent class"

(9) This and super

A: Difference

"This represents the corresponding reference for this class"

"Super represents the identity of the parent storage space (which can be understood as a parent class reference that can manipulate members of the parent class)"

B: Use

"Call member variable"

this. member variable--Call member variable of this class

Super. Member variable--Call the member variable of the parent class

"Call constructor Method"

This (...) --Call the construction method of this class

Super (...) --Call the constructor method of the parent class

"Call member Method"

This. member Method--calling member methods of this class

Super. Member Method--Call the member method of the parent class

3. Override of Method (override)

(1) Definition:

A method that is identical to a method declaration in a parent class appears in a subclass

(2) Application:

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 that is specific to subclasses is defined.

(3) How to use:

(4) Precautions

A: Private methods in the parent class cannot be overridden

"Because the parent class is private, the subclass simply cannot inherit"

B: When subclasses override parent class methods, access permissions cannot be lower

"Sub-class permissions are best consistent with the parent class"

C: Parent class static methods, subclasses must also be overridden by static methods

"This is not a rewrite, but the phenomenon is true."

Java Series Learning (vii)-Inheritance

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.