Calling Super-class Constructors in Java Programming
Previous | Home | Next |
Constructors use super to invoke the superclass's constructor. If a constructor uses super, it must use it in the first line; otherwise, the compiler will complain.
Constructor Create an instance of a class .
for constructor Modifiers Cannot be abstract, final, native, static, or synchronized
No return type, not even void is required for constructor.
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
public class Human
{
private String sex;
private int age;
public Human(String sex) // constractor with string parameter
{
this.sex = sex; // this keyword call the constructor
}
}
public class Female extends Human
{
private String hairStyle;
public Female(String hairStyle, String sex)
{
super(sex); // constructor calling using super keyword
this.hairStyle = hairStyle;
}
}
Previous | Home | Next |