The examples in this article describe the construction methods in Java inheritance. Share to everyone for your reference. Specifically as follows:
How to construct in inheritance:
1. The construction method of the base class must be called during the construction of the subclass.
2. Subclasses can use Super (argument_list) to invoke the constructor method of the base class in their own constructor methods.
2.1. Use this (argument_list) to invoke another construction method of this class.
2.2. If you call super, you must write the first line of the subclass construction method.
3. If the constructor method of the base class is not displayed in the subclass's construction method, the system defaults to calling the base class's parameterless construction method.
4. If the subclass constructor does not display the call base class constructor, and the base class has no constructor without parameters, the compilation fails.
The instance code is as follows:
Class superclass{
private int n;
Superclass () {
// System.out.println ("superclass ()");
}
Superclass (int n) {
System.out.println ("superclass (int n)");
THIS.N = n;
}
}
Class Subclass extends superclass{
private int n;
Subclass () {
super ();
System.out.println ("superclass");
}
Subclass (int n) {
System.out.println ("subclass (int N):" +n);
THIS.N = n;
}
}
public class testsupersub{public
static void Main (String args[]) {
//subclass sc = new subclass ();
Subclass SC2 = new subclass (MB);
}
}
Verify the above syntax in turn.
I hope this article will help you with your Java programming.