If you don't understand this in the first place, it's kind of a mess.
Apparently, the image is a method.
I don't know if it's correct, but when I looked it up, I could only find an explanation such as ~ used sometimes.
I don't know what happens when I use super, so I write it like this and get an error
public class Oya {
  private String name;
  private int score;
  
//constructor
  public Oya(String name, int score) {
    this.name = name;
    this.score = score;
  }
  //Getter that allows you to read the value of a variable from a child class
  public String getName() {
    return this.name;
  }
  
  //Getter that allows you to read the value of a variable from a child class
  public int getScore() {
    return this.score;
  }
}
public class Ko extends Oya {
  //Child class constructor
  public Ko() {
    // super(name); //← No
    // super(score); //← No
    super(name, score); //Write in the same way as when passing arguments to a method
  }
  public void sayHello() {
    //Since name is private in the Oya class, this.Cannot be accessed by name etc.
   //If you write a getter in the Oya class, it will return the value of name using the method.
    System.out.println("hello! " + this.getName());  //← Call the getName method of the Oya class at the end and receive the value of name
  }
}
public static void main(String[] args) {
  Ko bob = new Ko("bob", 10);
  Ko.sayHello();
}
This worked.
If you write super (name, score), the compilation error disappears and the constructor seems to work properly.
I just couldn't think of this way of writing, and the example of using super came out with only one argument, so I'll leave it as a memo.
I misunderstood that I would pass variables one by one to the constructor of the Oya class by writing super (argument).
This is an amateur article, so please point out any mistakes.
Recommended Posts