Explain several USES of super in Java and how it differs from this

  • 2020-04-01 02:34:06
  • OfStack

1. If the constructor of a subclass references super, it must put super first in the function


class Base {
Base() {
System.out.println("Base");
}
}

public class Checket extends Base {
Checket() {
super();//Calls to the parent constructor must be placed in the first statement of the method
System.out.println("Checket");
}
 
public static void main(String argv[]) {
Checket c = new Checket();
}
}

If you want to use super to inherit from the parent class, but not in the first line, then the statement before super must be to satisfy the statement that you want to complete some behavior, but also use super to inherit from the parent class constructor. Then all the changes you've made will go back to where they were before, which is to say, the parent constructor again.

 

2. In Java, you sometimes encounter a member variable or method in a subclass with the same name as a member variable or method in a superclass (sometimes called a parent class). Because the member variable or method name in a subclass has a high priority, the member variable or method with the same name in the subclass hides the member variable or method of the superclass, but if we want to use the member variable or method in the superclass, we need to use super.


class Country {
String name;
 
void value() {
name = "China";
}
}
 
class City extends Country {
String name;
 
void value() {
name = "Hefei";
super.value();//When this method is not called, super.name returns the value null of the member variable of the parent class
System.out.println(name);
System.out.println(super.name);
}
 
public static void main(String[] args) {
City c=new City();
c.value();
}
}

In order to refer to the member variable name and method value() in the parent class in the subclass, super, super. Name and super. Value () are used in the code.

Also, notice that super.name calls the value of the member variable,


class Country {
String name="xianfan";
 
String value(String name) {
name = "China";
return name;
}
}
 
class City extends Country {
String name;
 
String value(String name) {
name = "Hefei";
super.value(" failure ");//When this method is not called, super.name returns the value null of the member variable of the parent class
System.out.println(name);
System.out.println(super.name);
return name;
}
 
public static void main(String[] args) {
City c=new City();
c.value(" successful ");
}
} 

The result is: Hefei

xianfan

At this point, the value returned by super.name is the value of the parent class member variable xianfan, and the super.value() method does not work.

 

3. Directly pass parameters with super:


class Person {
public static void prt(String s) {
System.out.println(s);
}
 
Person() {
prt("A Person.");
}
 
Person(String name) {
prt("A person name is:" + name);
}
}
 
public class Chinese extends Person {
Chinese() {
super(); //Call the superclass constructor (1)
prt("A chinese.");// (4)
}
 
Chinese(String name) {
super(name);//Call the constructor of the superclass with the same parameter (2)
prt("his name is:" + name);
}
 
Chinese(String name, int age) {
this(name);//Call the constructor that currently has the same parameter (3)
prt("his age is:" + age);
}
 
public static void main(String[] args) {
Chinese cn = new Chinese();
cn = new Chinese("kevin");
cn = new Chinese("kevin", 22);
}
} 

The result is: A Person.

A Chinese.

A person name is: Kevin

His name is Kevin

A person name is: Kevin

His name is Kevin

His age is: 22

In this program, this and super are no longer ". "linked to a method or member, as they used to be, but are directly followed by the appropriate parameters, so their meaning changes. The arguments after super are used to call constructors of the same form in the superclass, such as 1 and 2. This followed by an argument calls the constructor that currently has the same argument, such as at 3. Of course, in various overloaded constructors in Chinese, various USES of this and super in general methods are still available. For example, at 4, you can replace it with "this.prt" (because it inherits the method in the parent class) or "super.prt" (because it is a method in the parent class and can be accessed by subclasses), and it still works correctly. But this seems like a bit of gilding the lily.

4. Similarities and differences between super and this:

1)super (parameter) : calls a constructor in the base class (which should be the first statement in the constructor) & PI;

2)this (parameter) : call another constructor formed in this class (should be the first statement in the constructor)
3)super: it refers to the member in the direct parent class of the current object (to access the member data or functions hidden in the direct parent class.     Member function name (argument)

4)this: it represents the name of the current object (in the program is prone to ambiguity, should use this to indicate the current object; This is used to specify the name of the member variable if the name of the member is the same.

5) the call to super() must be written on the first line of the subclass constructor or the compilation will not pass. The first statement of each subclass constructor implicitly calls super(), and if the parent class does not have a constructor of this form, it will report an error at compile time.

6) super() is similar to this() except that super() calls the parent constructor from a subclass, and this() calls other methods in the same class.

7) both super() and this() need to be placed on the first line of the constructor.

8) although you can call one constructor with this, you cannot call two.

9) this and super cannot appear in the same constructor at the same time, because this will inevitably call other constructors, other constructors will also have the existence of super statement, so there is the same statement in the same constructor, it will lose the meaning of the statement, the compiler will not pass.

10) this() and super() both refer to objects, so neither can be used in a static environment. Include: static variable,static method,static statement block.

11) essentially, this is a pointer to the object, whereas super is a Java keyword.


Related articles: