Error: Implicit super constructor People () is undefined for default constructor. Must define an explicit constructor, explicitconstructor

Source: Internet
Author: User

Error: Implicit super constructor People () is undefined for default constructor. Must define an explicit constructor, explicitconstructor

When a subclass inherits the parent class, if the parent class does not define a constructor with parameters, the subclass can inherit the default constructor of the parent class.

When a constructor with parameters is defined in the parent class, the subclass must explicitly call the constructor of the parent class.

If the subclass also wants to call the default constructor of the parent class, it must explicitly declare the default constructor in the parent class.

1 package com. gaohui; 2 3 public class Test {4 public static void main (String [] args) {5 Man = new man (24, "Tom"); 6 Man. eat (); 7 man. eat ("Tom"); 8 man. exercise (); 9 Woman woman = new Woman (); 10 11} 12 13} 14 15 class People {16 private int age; 17 private String name; 18 private String sex; 19 20 public People () {21 // if the parent class does not declare the default constructor, the constructor defined by the parent class must be explicitly called during subclass inheritance 22} 23 24 public People (int age, String name) {25 System. out. println ("constructor 1 is executed! "); 26} 27 28 public void eat () {29 System. out. println (" People need to eat! "); 30} 31 32 public void eat (String name) {33 System. out. println (name +" needs to eat! "); 34} 35 36 37} 38 39 class Man extends People {40 public Man (int age, String name) {// because the constructor of the parent class has parameters, must explicitly call the parent class Method 41 super (age, name); 42} 43 44 public void Exercise () {45 System. out. println ("I love doing exercise! "); 46} 47} 48 49 50 class Woman extends People {51 52}

 

Related Article

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.