The calling rule for a parent class constructor in a child class
1, if not explicitly specified, the constructor in the subclass calls the non-parametric constructs in the parent class by default
Test code
Packageday07;Importjava.sql.Date;ImportJavax.xml.bind.annotation.adapters.XmlJavaTypeAdapter.DEFAULT;classparent{PrivateString name; Private intAge ; PublicParent () {System.out.println ("Person ()"); } PublicParent (String name) {System.out.println ("Person (String name)"); This. SetName (name); } PublicParent (String name,intAge ) {System.out.println ("Person (String name, int age)"); This. SetName (name); This. Setage (age); } Public voidsetName (String name) { This. Name =name; } Public voidSetage (intAge ) { This. Age =Age ; }}classChildextendsparent{ PublicChild () {System.out.println ("Child ()"); } PublicChild (String name) {System.out.println ("Child (String name)"); } PublicChild (String name,intAge ) {System.out.println ("Child (String name, int age)"); }} Public classstatickey{ Public Static voidMain (String args[]) {Child C1=NewChild (); Child C2=NewChild ("Zhang San"); Child C3=NewChild ("Zhang San", 15); }}
Output
Person ()
Child ()
Person ()
Child (String name)
Person ()
Child (String name, int age)
If there is no parameterless construct in the parent class at this time, the compilation will error, such as:
The code above compiles with the following error message:
Extendsdemo.java:13: Error: The constructor parent in the class parent cannot be applied to the given type;
Public Child () {
^
Required: String,int
Found: no parameters
Cause: The actual parameter list and formal parameter list length are different
1 errors
2. If there are multiple constructors in the parent class, you can explicitly invoke the specified constructor in the parent class using the "Super (parameter list)" format, as the above code can eliminate the above compilation error after making the following modifications in the child class.
Class Child extends parent{
Public Child () {
super ("Bing", +);
System.out.println ("Child ()");
}
}
Rules for the override of parent-class functions in sub-classes
1. When subclasses overwrite functions in the parent class, access rights can only be extended, not reduced
2. The function of private permission in the parent class cannot overwrite the quilt class
Test code:
classparent{ Public voidprint () {System.out.println ("Parent:print ()"); } Public voidFun () { This. Print (); }} classChildextendsparent{ Public voidprint () {System.out.println ("Child:print ()"); }} Public classextendsdemo01{ Public Static voidMain (String args[]) {Child C1=NewChild (); C1.print (); C1.fun (); }}
Modify the permissions for the print () method in the parent class to private, default, and public, and then overwrite in the child class, as shown in the following table.
------------------------------------------
Parent class Subclass Output
------------------------------------------
Private default Child:print ()
Parent:print ()
Private public Child:print ()
Parent:print ()
Default default Child:print ()
Child:print ()
Default public Child:print ()
Child:print ()
Public public Child:print ()
Child:print ()
------------------------------------------
Rules inherited in Java