Constructor in java inheritance

Source: Internet
Author: User

Constructor in java inheritance
Constructor in inheritance 1. The constructor of the base class must be called during the constructor of the subclass. 2. Subclass can use super (argument_list) in its own constructor to call the constructor of the base class. 2.1 use this (argument_list) to call another constructor of this class. 2.2 If super is called, it must be written in the first line of the subclass constructor. 3. If the constructor of the base class is not displayed in the constructor of the subclass, the system calls the non-parameter constructor of the base class by default. 4. If the subclass constructor does not show that the base class constructor is called, and the base class does not have a constructor without parameters, a compilation error occurs. Copy the code class SuperClass {private int n; // SuperClass () {// System. out. println ("SuperClass ()"); //} SuperClass (int n) {System. out. println ("SuperClass (int n)"); this. n = n ;}} class SubClass extends SuperClass {private int n; SubClass () {super (300); System. out. println ("SuperClass");} SubClass (int n) {System. out. println ("SubClass (int n):" + n); this. n = n ;}} public class TestSuperSub {public static void main (String args []) {// SubClass SC = new SubClass (); subClass sc2 = new SubClass (200 );}

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.