This is the 13th day of my participation in the August More Text Challenge.More challenges in August

💝 Use of the super keyword in Java (in instance methods)

Both super and this can be used in instance methods, and neither can be used in static methods. “this” can be omitted in most cases, except when distinguishing local variables from instance variables in a method.

So when can “super” be omitted and when can’t?

// Book public class Book {// Book name String name; Constructor public Book(){super(); } public Book(String name){ super(); this.name = name; }}Copy the code
Public class PaperBook extends Book {// Constructor public PaperBook(){super(); } public PaperBook(String name){ super(); this.name = name; Public void printName(){system.out.println ("this.name-> this.name: "+ this.name); System.out.println("super.name-> "+ super.name); }}Copy the code
Public class BookTest {public static void main(String[] args) {PaperBook book1 = new PaperBook(" zero basic Java volume I"); book1.printName(); }}Copy the code

The running results are as follows:

Figure 1: Super and this

We find that super.name and this.name are the same in printName(). Why is this?

Please look at the memory diagram of the program execution above:

Figure 2: Memory diagram after the constructor of the parent class completes

Figure 3: Memory diagram of a subclass after its constructor completes

The above memory structure shows that this.name and super.name are actually the same memory space, so their output is exactly the same.

Next, let’s modify the above PaperBook class:

Public class PaperBook extends Book {String name; // Constructor public PaperBook(){super(); } public PaperBook(String name){ super(); this.name = name; Public void printName(){system.out.println ("this.name-> name: "+ this.name); System.out.println("super.name-> "+ super.name); }}Copy the code

The running results are as follows:

Figure 4: Difference between super and this

Super. name = null; super.name = null;

Figure 5: Memory diagram after the constructor of the superclass Book is executed

Figure 6: Memory diagram after constructor execution of subclass PaperBook

As can be clearly seen from the above memory diagram, the constructor of the superclass Book assigns null to super.name and the constructor of the subclass PaperBook assigns “zero basic Java volume I” to this.name. Since the variable name with the same name is defined in the subclass PaperBook, there are two names in the current object, one is inherited from the parent class, and the other is its own. If you want to access the name inherited from the parent class at this time, you must use super.name. Either name or this.name accesses the current object’s own name.

Through the above study, do you know when super can not be omitted? If you want to access the instance variable of the parent class in a subclass, super cannot be omitted. Is this true of instance methods?

We can test it, look at the code:

Public toString(){return "I am a gold member "; } public void test(){ System.out.println(super.toString()); System.out.println(this.toString()); System.out.println(toString()); }}Copy the code
public class VipTest { public static void main(String[] args) { Vip vip = new Vip(); vip.test(); }}Copy the code

The running results are as follows:

Figure 7: Run results

Through the above tests, the final conclusion is drawn: if there are instance variables or instance methods with the same name in the parent class and subclass, super cannot be omitted if you want to access instance variables or instance methods in the parent class in the subclass, and other cases can be omitted.