Java constructor-why does a subclass sometimes need to call Super in the constructor?

Source: Internet
Author: User
When Java instantiates an object, if super () is not explicitly used (), the system first calls the non-argument constructor of the parent class (not the one corresponding to the number of self-constructed function parameters) and then calls the constructor of the subclass. If the parent class is not an object class, the super () can only be used in the first row of the constructor. It is invalid in other places. Generally, the constructor does not need to write super (), however, when a class has multiple constructor functions, it is often necessary to explicitly call Super () for ease of understanding ().

The running result of the following sample code is:
I am Father
I am Son: Baidu
I am Father
I am Son

Sample Code:

 

1. Public class test {
2. Public static void main (string [] ARGs ){
3. New son ("Baidu ");
4. New son ();
5 .}
6 .}
7.
8. Class farther {
9. Public farther (){
10. system. Out. println ("I am Father ");
11 .}
12.
13. Public farther (string name ){
14. system. Out. println ("I AM FATHER:" + name );
15 .}
16 .}
17.
18. Class son extends farther {
19. Public son (){
20. system. Out. println ("I am Son ");
21 .}
22. Public son (string name ){
23. // super ("google ");
24. system. Out. println ("I am Son:" + name );
25 .}
26 .}

 

 

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.