In the subclass, must I access the method with parameters of the parent class ?, A subclass accesses a private member of the parent class.

Source: Internet
Author: User

In the subclass, must I access the method with parameters of the parent class ?, A subclass accesses a private member of the parent class.

Abstract class Person {private int age; private String name;Public Person (intAge, String name){This. age = age; this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public String getName () {return name;} public void setName (String name) {this. name = name;} public abstract void want ();} class Student extends Person {private int score; public int getScore () {return score;} public void setScore (int score) {this. score = score ;}Public Student (int age, String name, intScore){Super (age, name );This. score = score;} public void want () {System. out. println ("name:" + getName () + "Age:" + getAge () + "score:" + getScore ());}} class Worker extends Person {private int money; public int getMoney () {return money;} public void setMoney (int money) {this. money = money ;}Public Worker (int age, String name, intMoney){Super (age, name );This. money = money;} public void want () {System. out. println ("name:" + getName () + "Age:" + getAge () + "Salary:" + getMoney ());}} public class ABSDemo01 {public static void main (String [] args) {Student s = new Student (10, "James", 100); s. want (); Worker w = new Worker (35, "Ming", 1000); w. want ();}}

Analyze the above Code:

The above code overwrites the constructor public Person (int age, String name) in the parent class ). If the sub-class does not access the parent class's constructor with parameters, the sub-class will access the non-argument constructor super () of the parent class by default (), however, the parent class has constructed a constructor with parameters,If there is no constructor in the parent class, it cannot be called, so compilation will fail.

Conclusion 1:
Java ConstructorCan only be called by new, The programmer cannot directly call, but it is availableSuper () indirect callThe class construction method isNot inherited!
Conclusion 2:
  If the sub-class constructor does not show that the parent class constructor is called, the system automatically calls the default constructor super () of the parent class ().

Conclusion 3:

  After a constructor is created, no default constructor is available. If no constructor is available, a constructor without parameters is created by default.

Conclusion 4:

When no constructor is provided for the class, the system automatically provides a constructor with no arguments for its implementation as null. Once a custom constructor is provided, the system will not provide this default constructor. To use it, the programmer must manually add it.

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.