preface

Today, there is no foreword, all are dry goods and hard goods, this article will certainly make you understand the use of super! Without further ado, let’s cut to the chase.

The use of super ()

A subclass overrides a parent class method

/** * parent *@description: father
 * @author: Zhuang Ba. Liziye *@create: * * / 2021-12-06 sets
public class father {

    private String fatherName = Little Daddy.;

    public void getName(a) {
        System.out.println("I am"+ fatherName); }}Copy the code
/** * subclass *@description: son
 * @author: Zhuang Ba. Liziye *@create: the 2021-12-06 15:07 * * /
public class son extends father{

    private String sonName = Big Head boy;

    @Override
    public void getName(a) {
        System.out.println("I am" + sonName);
        super.getName();
    }

    public static void main(String[] args) {
        father b = newfather(); b.getName(); }}Copy the code

Let’s run the code and the result is ๐Ÿ‘‡The overridden getName() method of the parent class must be specified by the super keyword if the overridden getName() method is called.

There will certainly be some friends “new way”, do not use super to specify, according to the principle of subclass first, equivalent to calling the getName() method in the subclass, also formed an endless loop. Let’s see what happens if we don’t use the super keyword ~ ๐Ÿ‘‡

A subclass overrides a parent class variable

/** * parent *@description: father
 * @author: Zhuang Ba. Liziye *@create: * * / 2021-12-06 sets
public class father {

    String name = Little Daddy.;
    
}
Copy the code
/** * subclass *@description: son
 * @author: Zhuang Ba. Liziye *@create: the 2021-12-06 15:07 * * /
public class son extends father{

    String name = Big Head boy;

    public void getName(a) {
        System.out.println("I am" + name);
        System.out.println("I am" + super.name);
    }

    public static void main(String[] args) {
        son s = newson(); s.getName(); }}Copy the code

Let’s run the code and the result is ๐Ÿ‘‡Subclasses at this time there was a field name as the parent class (or a parent field is hidden), in order to get the parent class of the field we have to add super, if you still want to play without super fancy, then certainly not an error, because at this point is a subclass of access to the inside of the field, but also doesn’t make sense.

The super keyword does not allow access to private variables and methods in the parent class. Private variables and methods belong to internal members of the parent class, and other objects cannot access their private members.

Use super in the subclass constructor

The compiler automatically calls the parent class’s no-argument constructor by adding super to the first line of the subclass constructor. Super can be omitted and must be in the first line of the subclass constructor. You can also use super to call the other constructor of the parent class, as long as the parameters are passed in accordingly.

/** * parent *@description: father
 * @author: Zhuang Ba. Liziye *@create: * * / 2021-12-06 sets
public class father {

    public father(String name) {
        System.out.println("This is the father class with a parameter constructor named:"+name); }}Copy the code
/** * subclass *@description: son
 * @author: Zhuang Ba. Liziye *@create: the 2021-12-06 15:07 * * /
public class son extends father{

    public son(String name) {
        super(name);
    }

    public static void main(String[] args) {
        new son("Big Head son and Small head Father."); }}Copy the code

Let’s run the code and the result is ๐Ÿ‘‡ There are two things to be aware of when using the super keyword in a subclass constructor:

(1) pay attention to a

/** * parent *@description: father
 * @author: Zhuang Ba. Liziye *@create: * * / 2021-12-06 sets
public class father {

    public father(String s) {}}Copy the code

If no constructor is written to a class, the JVM generates a constructor with no arguments by default. In inheritance, because in the subclass constructor, default to invoke the first statement of the parent class no arguments constructor (namely defaults to super (), usually omitted) this sentence, so when the parent class defines a constructor, but does not define a no-parameter constructor, the IDE will force us to define a constructor of the same parameter types.

(2) pay attention to the two

/** * parent *@description: father
 * @author: Zhuang Ba. Liziye *@create: * * / 2021-12-06 sets
public class father {

    public father(String s) {}}Copy the code

Although we define a constructor in our subclass, super() is called by default in this constructor, but it does not exist in the parent class, so a compilation error occurs.

In the constructor of a subclass, whenever a constructor is not specified to call the parent class through super, the default constructor is called without arguments, so make sure that the parent class has the corresponding constructor.

summary

My experience is limited, some places may not be particularly in place, if you think of any questions when reading, welcome to leave a message in the comments section, we will discuss one by one ๐Ÿ™‡

Please take a thumbs up and a follow (โœฟโ—กโ€ฟโ—ก) for this article ~ a crab (โ—’โ—ก’โ—)

If there are mistakes in the article, welcome to comment correction; If you have a better, more unique understanding, you are welcome to leave your valuable ideas in the comments area.

Love what you love, do what you do, listen to your heart and ask nothing