About Java Polymorphism super ()

I have investigated polymorphism again, so I will summarize it. This time I want to deepen and understand super ()

** Target **

--Someone who writes Java somehow

super () is the constructor of the parent class

** Conclusion **

I will try it with a concrete example

Concrete example

Person.java


public class Person {
    Person() {
        System.out.println("I am a human." + );
    }
}

Student.java


public class Student extends Person {
    Student() {
        System.out.println("I am a student.");
    }
}

Main.java


public class Main {
    public static void main() {
        new Student();
    }
}
I am a human.
I am a student.

Commentary

The constructor of the Student class does not have a description to output "Human.", But it is displayed. In other words, the constructor of the Student class that inherits Person

Person.java


public class Student extends Person {
    Student() {
        super();
        System.out.println("I am a student.");
    }
}

It looks like this.

(It's not actually done, but if you write it explicitly, it looks like this. When you create an instance of an inherited class, the constructor of the parent class is called automatically)

This super () is the constructor of the parent class.

Add argument

Then add the arguments to the constructor of the parent class

Person.java


public class Person {

    private String name;

    Person(String name) {
        this.name = name;
        System.out.println(name + "is.");
    }
}

Main.java


public class Main {
    public static void main() {
        new Student();
    }
}

Yes. There is a compilation error. This is because the constructor of the Person class requires an argument. In such a case, you need to call super () with an argument in the Student class.

Student.java


public class Student extends Person {
    Student(String name) {
        super(name);
        System.out.println("I am a student.");
    }
}

Main.java


public class Main {
    public static void main() {
        new Student("Yamada");
    }
}
This is Yamada.
I am a student.

Student inherits from Person, so it also has a name field

Main.java


public class Main {
    public static void main() {
        Student student = new Student("Yamada");
        System.out.println(student.name);
    }
}
This is Yamada.
I am a student.
Yamada

Recommended Posts

About Java Polymorphism super ()
[Java] Polymorphism
About polymorphism
java (polymorphism)
About Java interface
[Java] About Java 12 features
[Java] About arrays
Something about java
About Java features
About Java threads
[Java] About interface
About Java class
About Java arrays
About java inheritance
About interface, java interface
About List [Java]
About java var
About Java literals
About Java commands
About Java log output
About Java functional interface
Java, about 2D arrays
About [Java] [StreamAPI] allMatch ()
About Java StringBuilder class
[Java] About Singleton Class
About Java method binding
[Java] About anonymous classes
About method splitting (Java)
[Java Silver] About initialization
About Java Array List
java (merits of polymorphism)
About inheritance (Java Silver)
About Java String class
About Java access modifiers
About Java lambda expressions
About Java entry points
About Java 10 Docker support
Personal summary about Java
[Java] About enum type
All about Java programming
About java abstract class
A note about Java GC
What I researched about Java 8
About an instance of java
What I researched about Java 6
[Gradle] About Java plug-in tasks
About Java variable declaration statements
What I researched about Java 9
[Java] About try-catch exception handling
About Java class loader types
[Java Silver] About equals method
[Java] About String and StringBuilder
What I researched about Java 7
About Alibaba Java Coding Guidelines
About Java class variables class methods
About Java Packages and imports
About abstract classes in java
[Android / Java] Learned about DataBinding
What I researched about Java 5
About Java static and non-static methods
[Introduction to Java] About lambda expressions