This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.

Four access modifiers

  • Four access modifiers

    • Public, default (do not write), protected, private
    • If the subclass and its parent are in the same package, then member modifiers for the parent class can be used as long as they are not private
    • If the subclass and its parent are not in the same package, the subclass can only use the protected and public members of the parent class

The keyword super

  • The keyword super

    • Use super in a Java class to call the specified operation in 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 parent class’s constructor in a subclass constructor
    • Pay attention to

      • This is especially true when a parent class has a member of the same name
      • The traceability of super is not limited to immediate superclasses
      • Super is used similarly to this, where this stands for a reference to an object of this class and super stands for the memory space identifier of the parent class
    • For example,
    • practice

      • The employed() method of the parent class ManKind and the “But Kids should study and no job!” method in the class Kids employed()
      • Question 2: Calculate the cylinder surface area in TestCylinder
      • Create a Circle object in TestCylinder, set the radius, and calculate the area of the Circle
    • Invoke the parent class’s constructor

      • By default, all constructors in a subclass access the constructor with hollow arguments in its parent class
      • When there is no constructor for empty arguments in the parent class, the constructor of a subclass must specify a call to the corresponding constructor in the parent class through this (argument list) or super (argument list) statement, and must be placed on the first line of the constructor. (When only the parent class has a parameter construct to use, the subclass must explicitly build a parameter construct to call the parent class’s parameter construct, and the constructor calling the parent class is written on the first line.)
      • If neither the parent class nor its constructor is explicitly called in the subclass constructor, and there is no constructor with no arguments in the parent class (of course there is no error if the parent class has both arguments and no arguments, because the subclass calls the parent class’s no-argument constructor by default), the compilation error occurs.
    • The difference between this and super

      • Note: Because both this and super call constructors must start on the first line, only one can be used