A constructor

  • Java has a special kind of method called “constructors,” also known as constructors, constructors.
  • The constructor ensures that each object is initialized.
  • The constructor is executed only once when the object is created.
  • The == constructor has the same name as the class name and returns no value. == The main function is to perform some initialization operations when creating an object.
  • If a class does not have a constructor, the JVM creates a constructor that takes no arguments at compile time. = =
  • Constructors can be overloaded

Method overloading

Each overloaded method has a unique argument list.

Method overloading features:

  • == The method names are the same ==
  • == Different parameter lists (different number, type, and order) ==
  • == is independent of return values and access modifiers ==

Methods to rewrite

  • Overrides occur between a subclass and a superclass, with the subclass overriding the methods of the superclass.
  • The overridden method must have the same method name, return value type, and argument list as the overridden method of the parent class. (Only rewrite method body)
  • A child class overrides a method with no less access than the parent class overrides a method.
  • Override methods are identified using the @override annotation.

Permission to access a modifier

== Precautions when super and this appear in methods ==

  • Super represents the default reference to an object of the immediate parent of the current object
  • This represents the default reference to the current object
  • Super and this appear in constructors only on the first line of the constructor and not at the same time
  • The this and super keywords are not allowed in class methods (that is, static modified methods)
  • In instance methods this super should not be the first line and can coexist.