Inheritance and Override

1: How to make a help document (understand)

  • (1) Write a class
  • (2) Add documentation comments
  • (3) It can be generated by the javadoc tool

javadoc -d directory-author -version ArrayTool.java

2: Learned the Math class through the API provided by JDK (master)
(1) API (Application Programming Interface)
application programming interface (help document)
(2) How to use it?
Please refer
to day08\code\02_How to use the JDK provided Help documentation\how to use help documentation.txt
(3) Math class

  • A: It is a class that operates on mathematics
  • B: There is no constructor because its members are static
  • C: generate random numbers
  •     public static double random(): [0.0,1.0)
  • D: How to generate a random number between 1-100
  •     int number = (int)(Math.random()*100)+1;
  • E: Guess the number game

3: Code block (understanding)
(1) Code enclosed in {}.
(2) Classification:

  • local code block
  •     Used to limit the life cycle of variables, release them early, and improve memory utilization.
  • Building code blocks
  •     The same code in multiple constructors can be placed here. Before each constructor is executed, the constructor code block is executed first.
  • static code block
  •     Initialize the data of the class and execute it only once.

(3) The sequence of static code blocks, construction code blocks, construction methods?
Static code blocks > Construction code blocks > Construction method

4: Inheritance (master)
(1) Extract the same members from multiple classes and define them into one in a separate class. Then let these multiple classes have a relationship with the independent class, and
these multiple classes have these contents. This relationship is called inheritance.
(2) How to express inheritance in Java? What is the format?

  • Represented by the keyword extends
  • Format:
  •       class subclass name extends parent class name{}

(3) The benefits of inheritance:

  • Improve code reusability
  • Improved code maintainability
  • Let the class and the class have a relationship, which is the premise of polymorphism

(4) Disadvantages of inheritance:
A: Enhance the coupling of classes. In this way, changes in a class will affect other classes related to this class.

  • Principle: low coupling, high cohesion.
  • Coupling: Class-to-Class Relationship
  • Cohesion: the ability to accomplish something on your own

B: breaking the encapsulation
(5) the characteristics of inheritance in Java

  • Classes in Java only support single inheritance
  • Multi-layer (heavy) inheritance (inheritance system) possible in Java

(6) Notes on inheritance:

  • Subclasses cannot inherit private members of parent class
  • A subclass cannot inherit the constructor of the superclass, but can access it through super
  • Do not inherit for partial functionality

What should I do if there is no constructor in the parent class? The
subclass uses super to explicitly call other constructors with parameters of the parent class .
Subclasses use this to call other constructors of this class .
Other constructs of this class must also first access the parent class construct .
Be sure to pay attention:
super(…) or this(….) must appear in the first statement , otherwise, there will be multiple initializations of the parent class data.

Notes on method overriding:

  • Private methods in parent classes cannot be overridden.
  • When a subclass overrides a superclass method, the access rights cannot be lower.
  • The static method of the parent class, the child class must also be overridden through the static method.

(In fact, this is not a method rewriting, but the phenomenon is indeed the case. As for why it is not a method rewriting, I will explain it in polymorphism)

(7) When to use inheritance?

  • Inheritance reflects the relationship of: is a.
  • With the assumption method, you can't use inheritance if you can't understand it.

(8) Membership A in Java inheritance
: member variables

  • The name of the member variable of the subclass is not the same as the name of the member variable in the parent class . This is too simple.
  • The name of the member variable of the subclass is the same as the name of the member variable in the parent class. How to access this?

The order in which the methods of subclasses access variables are searched:

  • Find it in the local scope of the subclass method , and use it if you have it.
  • Find it in the member scope of the subclass , and use it if you have it.
  • Find it in the member scope of the parent class , and use it if there is.
  • If it is not found, an error will be reported.

B: Constructor
a: The constructor of the subclass will access the parameterless constructor of the parent class by default, so that the subclass can access the initialization of the parent class data.
b: What if there is no parameterless constructor in the parent class?

  • Subclasses use super to explicitly call the parameterized constructor.
  • The subclass calls other constructs of itself through this, but there must be one that accesses the superclass's construct.
  • Let the parent class provide a no-argument constructor.

C: member method

  • The name of the member method of the subclass is not the same as the name of the member method in the parent class , this is too simple
  • The member method of the subclass has the same name as the member method in the parent class. How to access this?

The lookup order for accessing a method via a subclass object:

  • Find it in the subclass , use it if there is one
  • Find it in the parent class, use it if you have it
  • If not found, report an error

(9) Two interview questions:

  • The difference between Override and Overload? Can Overload change the return value type?
  • The difference between this and super and their respective roles?

What do the this keyword and the super keyword stand for? And their respective usage scenarios and roles.

  • this: represents the object reference of the current class
  • super: The identifier representing the storage space of the parent class. (It can be understood as a reference to the parent class, through which you can access the members of the parent class)

Scenario:
 Member Variables:

  • this. member variable
  • super. member variable

  Construction method:

  • this(...)
  • super(...)

  Member method:

  • this.member method
  • super.member method

(10) Interview questions for data initialization

  • The initialization process of a class
  • The construction execution process of the child parent class
  • Hierarchical initialization

(11) Cases:
A: The case of students and teachers
Before
inheritance After inheritance
B: Analysis and realization of the case of cats and dogs

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325853944&siteId=291194637