Java object-oriented (four): inheritance

inherit

The concept of inheritance:

  • Inheritance means that one class can be constructed by another class. The original class is the parent class, and the new class is the child class.
  • Java only supports single-root inheritance, that is, each class can only have one direct parent class. Multiple inheritance is not allowed.

Features:

  • The subclass will automatically have all the non-private modified properties and methods of the parent class.
  • extends keyword: inheritance relationship, which means that a class inherits from another class. The left side of extends is the child class, and the right side is the parent class. The parent class is also called: super class and base class. The child class inherits the parent class and automatically has all the members (member variables and methods) of the parent class, which does not mean that it can access all the content of the parent class. The private properties and methods of the parent class cannot be accessed.

benefit:

  • The emergence of inheritance improves code reusability and improves development efficiency.
  • The emergence of inheritance creates a relationship between classes and forms polymorphism.

note:

  • Static methods can be inherited (strictly called hidden), but cannot be overridden, so static methods cannot be polymorphic.
  • The whole inheritance system is more abstract as you go up, and more concrete as you go down.
  • The construction method of the parent class cannot be inherited.

1. Super keyword

Significance : Use super in java class to refer to the components of the parent class.

  • super can be used to access properties defined in the parent class.
  • super can be used to call member methods defined in the parent class.
  • super can be used to call the construction method of the parent class in the subclass construction method.
  • The traceability of super is not limited to the immediate parent class.

usage:

  • 1. Use super to call the construction method of the parent class (the call to super must be the first statement in the construction method)
  • 2. Use super to specify the members (methods and properties) of the parent class.

analysis:

  • In the JAVA class, use super to refer to the components of the parent class, and use this to refer to the current object. If a class inherits from another class, when we new the instance object of the subclass, there will be a parent class in the subclass object. Object.
  • The subclass constructor will automatically call the default constructor super() of the parent class; if you want to explicitly call a certain constructor of the parent class, you can use the super keyword to call it. super(200);

Note :

  • This() and super() cannot be used in the same method at the same time, because the requirements must be on the first line.
  • If the constructor of the parent class is not specified in the subclass, then the subclass is calling the default constructor of the parent class. If the parent class does not have a default parameterless constructor (the parent class only writes the constructor with parameters) Method, the default construction method is lost), the subclass will report an error and the compilation fails.

2. Method rewriting (overwriting)

overload overload
method override (overwrite) :overWrite override

Interview question :
What are overloading and rewriting? The difference between the two?

  • Overloading is a method with the same method name and different parameter lists in the same class. In method overloading, overriding is generated in the parent-child relationship. The child class inherits the parent class, and the method of the parent class is rewritten, and The method name, parameter list, and return value must all be the same. The modifier of the subclass needs to be the same or wider than the parent class.

Why inherit : Mainly for code reuse. If the method gets smaller and smaller through inheritance, it violates code reuse.
Insert picture description here
Insert picture description here

Third, the final keyword

  • Final modified class, the final modified class cannot have subclasses.
  • Final modification method, the final modification method cannot be overridden.
  • Final modified variables, variables modified by final are called constants. If there is no assignment at the time of declaration, it can be initialized in the constructor. No more assignments can be made elsewhere.

Four, concluding remarks

Continue studying…

I hope to communicate more, pay more attention, and achieve dreams together

Guess you like

Origin blog.csdn.net/zhangzhanbin/article/details/111654256