Java constructor When you construct a subclass, you are bound to call the constructor method of the parent class. "Private property is inherited?" "Problem

Source: Internet
Author: User

"Error:implicit Super constructor Pet () is undefined. Must explicitly invoke another constructor "

 Remember one point: when you construct a subclass, you are bound to call the constructor of the parent class. Because the elements in the parent class also need to be initialized.
So the parent class either has a default parameterless construct, so Java automatically calls this parameterless construct. If the parent class does not have a parameterless construct, then you call the construction of the parent class by Super () in the construction of the child class.

The reference code is as follows;

    1. Package practise02_2;
    2. /*
    3. * Subclasses overriding parent class methods
    4. */
    5. public class Pet {
    6. Private String PetName;
    7. Private String ownername;
    8. Pet () {}
    9. Public Pet (String petname,string ownername) {
    10. System.out.println ("Wweqwq");
    11. This.petname=petname;
    12. This.ownername=ownername;
    13. }
    14. public void Playwithowner () {
    15. System.out.println (Getpetname () + "Being and Master" +getownername () + "Play");
    16. }
    17. Public String Getpetname () {
    18. return petname;
    19. }
    20. public void Setpetname (String petname) {
    21. This.petname = PetName;
    22. }
    23. Public String Getownername () {
    24. return ownername;
    25. }
    26. public void Setownername (String ownername) {
    27. This.ownername = ownername;
    28. }
    29. }

    1. when a subclass is instantiated, it must initialize the parent class variable, which is called the parent class constructor **********************
    2. Package practise02_2;
    3. public class Cat extends Pet {
    4. Private String PetName;
    5. Private String ownername;
    6. Public Cat (String petname, String ownername) {
    7. Super (PetName, ownername); //comment out will error! Why?? When instantiating subclasses, the attributes in the parent class also need to be initialized!! Error resolved after adding parameterless construction method to parent class
    8. This.petname=petname;
    9. This.ownername=ownername;
    10. }
    11. public void Playwithowner () {
    12. System.out.println (Getpetname () + "Being and Master" +getownername () + "Play");
    13. }
    14. }

    1. "The private property of the parent class can be inherited??? "********
    2. Package practise02_2;
    3. public class Dog extends pet{
    4. It is worth noting that although there is no subclass attribute defined here, it does not mean that the subclass inherits the private property of the parent class
    5. Subclasses simply call the constructor of the parent class, and when the child class is instantiated, the value is passed to the parameter of the subclass constructor method
    6. As can be verified: the formal parameters of the subclass construction method are different from the parent class, and there is no relationship between the two
    7. Public Dog (String A, string b) {//Parameter only, no practical meaning
    8. Super (A, b);
    9. TODO auto-generated Constructor stub
    10. }
    11. public void Playwithowner () {
    12. System.out.println (Getpetname () + "Being and Master" +getownername () + "Play");
    13. }
    14. }

    1. main function ***************
    2. Package practise02_2;
    3. public class Test {
    4. public static void Main (string[] args) {
    5. Cat Cat=new Cat ("Jen", "Tom");
    6. Dog Dog=new Dog ("Wang Choi", "Tom");
    7. Cat.playwithowner ();
    8. Dog.playwithowner ();
    9. }
    10. }
    11. Run result ***********

Java constructor When you construct a subclass, you are bound to call the constructor method of the parent class. "Private property is inherited?" "Problem

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.