Java Learning Note Seven---the influence of the parent class construction method on the child class with no parameters

Source: Internet
Author: User

The subclass does not inherit the constructor of the parent class, but the constructor of the parent class affects the creation of the child class construction method. Specifically, it is:

①. When the parent class does not have a parameterless constructor method, the subclass cannot have a parameterless construction method, and the parent class construction method must be explicitly called as Super (parameter) in the subclass construction method. Otherwise, the following error will occur:

Implicit super constructor person () was undefined for default constructor. Must define an explicit constructor

When a subclass explicitly calls Super (parameter) in a parameter-constructed method, the following error occurs if you write a non-parametric construction method:

Implicit super constructor person () is undefined. Must explicitly invoke another constructor

②. When the parent class has no parameter construction method, the subclass can have no parameter construction method, or there can be a parameter construction method, in a parameter construction method, you can explicitly call the parent class constructor method or do not call. That is, at this point, the subclass is relatively free to create the constructor method.

Here is a simple example:

There are two classes, person class, and student class, and the student class inherits from the person class. The construction method for two classes is described in the code.

Person class:

Package Human;public class Person {String name;int age; String Gender;private string hobby;public person () {}public person (string n, String g) {this.name = N;this.gender = g;} public person (string n, int A, string g, String h) {this.name = N;this.age = A;this.gender = G;this.hobby = h;} public void SetName (String n) {this.name = n;} public void Setage (int a) {this.age = A;} public void Setgender (String g) {this.gender = g;} public void Sethobby (String h) {this.hobby = h;} Public String GetName () {return this.name;} public int getage () {return this.age;} Public String Getgender () {return this.gender;} Public String Gethobby () {return this.hobby;} public void Informationprint () {System.out.println ("My name is" +this.getname ()); System.out.println ("I Am" + this.getage () + "years old"), if (this.getgender () = = "female") System.out.println ("I Am a Girl" ), ElseIf (this.getgender () = = "Male") System.out.println ("I am A Boy"), ElseSystem.out.println ("Something is wrong!"); System.out.println ("My hobby is" + thIs.hobby);}} 

Student class:

Package Human;public class Student extends person {string Stunumber;int score;public Student () {}public Student (string n, String g) {super (n,g);} Public Student (string n, int A, string g, String h) {super (n,a,g,h);} Public Student (String sN, int s) {This.stunumber = Sn;this.score = s;} Public Student (string n, String g, string sN, int s) {super (n,g); this.stunumber = Sn;this.score = s;} Public Student (string n, int A, string g, String h, string sN, int s) {super (n,a,g,h); this.stunumber = Sn;this.score = s;} public void Setstunumber (String num) {this.stunumber = num;} public void SetScore (int s) {this.score = s;} Public String Getstunumber () {return this.stunumber;} public int Getscore () {return this.score;} public void Informationprint () {super.informationprint (); System.out.println ("My number is" + this.stunumber); System.out.println ("My score is" + This.score);}

Test class:

Package Human;public class Testmain {public static void main (string[] args) {person xiaoxip = new Person ("XIAOXIP", +, "fem Ale "," Piano "); Person XIAONANP = new Person ("XIAONANP", "male"); Student Xiaoxis = new Student ("Xiaoxis", "90", "female", "Piano", "124", and "+"); Student Xiaonans = new Student ("Xiaonans", "male", "123", 98); Xiaoxip.informationprint (); Xiaoxis.informationprint (); Xiaonanp.informationprint (); Xiaonans.informationprint ();}}

To learn: an issue with the access permission modifier.

Java Learning Note Seven---the influence of the parent class construction method on the child class with no parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.