[Java] How to write when passing two or more arguments to super

What is super?

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.

If you think of it as a method, it's easy to imagine how to write the arguments.

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.

Pass two or more arguments like super (argument 1, argument 2)

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

[Java] How to write when passing two or more arguments to super
How to write java comments
How to write and notes when migrating from VB to JAVA
Studying Java # 6 (How to write blocks)
How to write Java variable declaration
[Introduction to Java] How to write a Java program
How to remote debug Java 9 or later
[Java] How to output and write files!
[Rails] How to write when making a subquery
Java Development Basics ~ How to Write Programs * Exercise 1 ~
[Java] Memo on how to write the source
How to write Java String # getBytes in Kotlin?
How to write Rails
How to write Scala from the perspective of Java
[Java] Types of comments and how to write them
java: How to write a generic type list [Note]
Create two or more line graphs using MPAndroidChart [Java]
How to write dockerfile
How to write docker-compose
How to write Mockito
How to convert A to a and a to A using AND and OR in Java
Summary of how to implement default arguments in Java
How to write migrationfile
How to write modern Java. Immutable Java, consider Null safety.
How to handle exceptions coolly with Java 8 Stream or Optional
Comparison of how to write Callback function (Java, JavaScript, Ruby)
When you want to explicitly write OR or AND with ransack
How to dynamically switch JDK when building Java in Gradle
How to install java9 on elementaryOS Freya or Ubuntu 14.04 LTS
How to write query option when using gem ruby-firebase (memorial)
[Java] How to use Map
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
How to write good code
Java --How to make JTable
How to use java Optional
Bit Tetris (how to write)
How to minimize Java images
[Java] How to use Optional ②
[Java] How to use removeAll ()
Great poor (how to write)
[Java] How to use string.format
How to use Java Map
How to write Junit 5 organized
How to write Rails validation
How to write Rails seed
How to use Java variables
[Ruby] How to write blocks
How to convert Java radix
How to write Rails routing
[Java] How to implement multithreading
[Java] How to use Optional ①
How to initialize Java array
How to change arguments in [Java] method (for those who are confused by passing by value, passing by reference, passing by reference)
Basics of Java development ~ How to write programs (variables and types) ~
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
How to write when installing Amazon Corretto 8 on CentOS 8 with Ansible.
How to solve the unknown error when using slf4j in Java
How to write offline real-time Java implementation example of F01 problem
How to write the view when Vue is introduced in Rails?