An explanation of the execution sequence in Java learning inheritance

Source: Internet
Author: User

Tag: Class Java class inheritance

code block (understanding)
(1) Code enclosed in {}.
(2) Classification:
A: Local code block
Used to limit the life cycle of variables, release early, and improve memory utilization.
B: Building Blocks of code
The same code in multiple construction methods can be put here, before each construction method executes, the construction code block is executed first.

C: Static code block

static{} initializes the data for the class and executes only once.

(3) Static code block, construct code block, construct method order problem?

Static code blocks > Constructing code blocks > Construction Methods

Class Student {static {System.out.println ("Student Static code block");} {System.out.println ("Student Construction Code block");} Public Student () {System.out.println ("Student constructor Method");}} Class Studentdemo {static {System.out.println ("Studentdemo Static code block");} public static void Main (string[] args) {System.out.println ("I am the Main method"); Student S1 = new Student (); Student s2 = new Student ();}}
The results of the operation are as follows:

/* Write the execution results of the program. <span style= "font-family:arial, Helvetica, Sans-serif;" >studentdemo Static code block </span> I am the Main method student static code block student construct code block student construct method student construct code block student construction Method */

Inherit (Master)
(1) The same members in multiple classes are extracted to define a separate class. Then let the multiple classes and the independent class produce a relationship,
These are the things that these classes have. This relationship is called inheritance.
(2) How is inheritance represented in Java? what is the format?
A: Use keyword extends to express
B: Format:
Class subclass name extends parent class name {}
(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. Such a change in a class will affect other classes related to that class.
Principle: Low coupling, high cohesion.
Coupling: Class-to-class relationships
The ability to accomplish something by oneself
B: Breaking the encapsulation
(5) Characteristics of inheritance in Java
Class in A:java only supports single inheritance
Multiple (Heavy) inheritance in B:java (inheritance system)
(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 do I use inheritance?
A: Inheritance embodies: is a relationship.
B: Using the Assumption method
(8) Member Relationships in Java inheritance
A: Member variables
A: The member variable name of the subclass is not the same as the member variable name in the parent class, direct access
B: The member variable name of the child class is the same as the member variable name in the parent class, how is this accessed?
Method of subclass Access variable lookup order: Nearest principle
Find the local scope of the subclass method and use it.
In the subclass of the member scope to find, there is the use.
In the member scope of the parent class, it is used.
If you can't find it, you'll get an error.
B: Construction method
A: The construction method of a subclass is accessed by defaultthe non-parametric construction method of the parent class

1: All constructor methods in subclasses access the constructor of the parent's hollow parameter by default
2: 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.
Note: The first statement of each constructor method of a subclass is: Super ();

Class Son extends Father {public Son () {//super (); System.out.println ("Son's method of non-argument construction");} Public Son (String name) {//super (); System.out.println ("The method of constructing the Son");}} Class ExtendsDemo6 {public static void main (string[] args) {//create object Son s = new Son (); System.out.println ("------------"); Son s2 = new Son ("Brigitte");}}
Operation Result:

The method of non-parametric construction of Father the method of non-parametric construction of son------------the method of father construction of son
B: What if there is no parameterless construction method in the parent class?
Subclasses use super to explicitly invoke the parameter construct (subclass with super (); Call the parent class constructor method can only be called in the first row of the constructor method)
Subclasses use this to invoke other constructs of their own, but there must be a construct that accesses the parent class.
Habits: It's best to have the parent class provide no-argument constructs every time

Class Father {/*public Father () {System.out.println ("non-parametric construction method of Father");} */public Father (String name) {System.out.println ("Father with parameter construction method");}} Class Son extends Father {public Son () {super ("casual"); System.out.println ("Son's non-parametric construction method");//super ("Give");} Public Son (String name) {//super ("random"); System.out.println ("The method of constructing the Son");}} Class ExtendsDemo7 {public static void main (string[] args) {son s = new Son (); System.out.println ("----------------"); Son ss = new Son ("Brigitte");}}
Operation Result:

The Father method for the construction of the parameters of the son the method of no-parameter construction----------------The method of father structure of the Son the method of the parameter structure of son
C: Member Method
A: The member method of the subclass does not have the same name as the member method in the parent class, directly accessing
B: The member method of the subclass is the same as the member method name in the parent class, how is this accessed?
To access the lookup order of a method through a subclass object: Nearest principle
Look in the subclass and use it.
Look in the parent class and use
If you can't find it, you'll get an error
(9) Two face questions:
The difference between A:override and overload? Can overload change the return value type?
What are the differences between B:this and super and their respective roles?

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)

How to use it?
A: Calling member variables
This member variable calls the member variable of this class
Super. member variables call member variables of the parent class
B: Call construction method
This (.. Parameters.) calling this class's constructor method
Super (.. Parameters.) calling the parent class's constructor method
C: Call member Method
This member method calls the member method of this class
Super. Member methods call the parent class's Members method
(10) Data initialization of the face question
A: Initialization process for a class

/* See the program Write results: A: Member variable nearest principle B:this and Super question this accesses members of this class super access the members of the parent Class C: The subclass constructor Method executes the parent class's parameterless constructor before executing the method D: Initialization of a class initializes a process member variable to initialize the default initialization display to initialize the constructor method initialization result: fuzi302010*/class fu{public int num = 10;public fu () {System.out.println ("Fu");}} Class Zi extends fu{public int num = 20;public Zi () {System.out.println ("Zi");} public void Show () {int num = 30; SYSTEM.OUT.PRINTLN (num); 30system.out.println (This.num); 20system.out.println (Super.num); 10}}class extendstest {public static void main (string[] args) {Zi z = new Zi (); Z.show ();}}


B: The construction of the child parent class execution process

/* Read the program to write the result: A: Static code block of a class, construct code block, construct method's execution flow static code block > construct code block > construct method B: Static content is loading static code block content with the load of the class will take precedence to C: The initialization of the parent class before the subclass initialization result is: Static code block fu static code block zi Construction code block Fu construction Method Fu construction code block Zi construction method Zi*/class Fu {static {System.out.println ("Static code block Fu");} {System.out.println ("Construct code block Fu");} Public Fu () {System.out.println ("construction Method Fu");}} Class Zi extends Fu {static {System.out.println ("Static code block Zi");} {System.out.println ("Construct code block Zi");} Public Zi () {System.out.println ("constructor method Zi");}} Class ExtendsTest2 {public static void main (string[] args) {Zi z = new Zi ();}}


C: Hierarchical initialization

/* See program Write Result: A: Question of member variable int x = 10; The member variable is the base type Student s = new Student (); Member variables are reference type B: Initialization of a Class A member variable initializes the default initialization (to the default value) to display initialization (give us the value assigned to the variable) constructor initializes the C: the initialization of the child parent class (hierarchical initialization) initializes the parent class and then initializes the subclass. Result: yxyz problem: Although the constructor method in the subclass has a super () initialization by default, it is not in that order. Instead, it follows a hierarchical initialization. It simply means initializing the parent class data before initializing the subclass data. */class X {y b = new Y (); X () {System.out.print ("X");}} Class Y {y () {System.out.print ("Y");}} public class Z extends X {y y = new Y (); Z () {//supersystem.out.print ("z");} public static void Main (string[] args) {new Z ();}}



An explanation of the execution sequence in Java learning 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.