this key difference with the super keyword

Why use the super keyword?

  Subclass To access the parent class method or variable.
  Subclass call the constructor of the parent class
Caution:

  Super keyword can only be used in the construction method or instance method, super keyword can not be used within a static method and a static code block.

  As this does not use keywords in the subclass constructor method, the parent class constructor with no arguments will be implicitly invoked;

  When you create a subclass object, the default constructor will first call the superclass no-argument can be constructed by super parent class keyword specifies the call. 

    (1) If the parent class constructor call is not specified in the constructor of a subclass, then the java compiler will add super () statement in the subclass constructor.

    When (2) super keyword to call the parent class constructor, the statement must be the first statement if the sub-class constructor.

this keyword is the main difference between the super keyword

  (1) on behalf of inconsistent things.

    super keyword represents the reference to the parent class space. (And do not represent the object, but represents an object in a piece of memory only) 

    this keyword represents the object belongs to the caller function.

  (2) the use of the premise inconsistent.

    super keyword must have to use inheritance.

    this keyword does not require the presence of inheritance can also be used

  (3) the difference between calling the constructor:

     Keywords are super constructor calls the parent class.

     this keyword is to call this class constructor.

  (4) super keyword and this can not occur at the same time calling other constructors in the same constructor. Because the first two statements require a statement.
 












 

Guess you like

Origin www.cnblogs.com/zhai113/p/11583794.html