Java class constructor, java class Constructor

Source: Internet
Author: User

Java class constructor, java class Constructor

The subclass cannot inherit the constructor (constructor or constructor) of the parent class, but the constructor of the parent class has parameters, then, the constructor of the parent class must be explicitly called using the super keyword in the constructor of the subclass and configured with an appropriate list of subordinate classes.

If the parent class has no parameter constructor, it is not necessary to call the parent class constructor with super in the constructor of the subclass. If the super keyword is not used, the system automatically calls the non-argument constructor of the parent class.

Public class InheritanceDemo {public static void main (String args []) {// SubClass SC = new SubClass (); SubClass sc2 = new SubClass (200); sc2.eat ();} public static class SuperClass {private int n; SuperClass () {System. out. println ("SuperClass ()");} SuperClass (int n) {System. out. println ("SuperClass (int n):" + n); this. n = n;} void eat () {}} public static class SubClass extends SuperClass {private int n; SubClass () {// super (300); System. out. println ("SuperClass");} public SubClass (int n) {// super (300); System. out. println ("SubClass (int n):" + n); this. n = n ;}}}

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.