This example shows you how to use the super
keyword to call a superclass constructor. The Female
class constructor calls its superclass constructor and initializes its own initialization parameters. The call to the superclass constructor must be done in the first line of the constructor in the subclass.
package org.kodejava.example.fundamental;
public class Human {
private String gender;
private int age;
public Human(String gender) {
this.gender = gender;
}
}
To call a superclass constructor we call super()
. In the case below we call the superclass constructor with one string variable as a parameter.
package org.kodejava.example.fundamental;
public class Female extends Human {
private String hairStyle;
public Female(String hairStyle, String gender) {
super(gender);
this.hairStyle = hairStyle;
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024