Java Review of Inheritance II

Source: Internet
Author: User

Calling the parent class constructor

Subclasses do not get the parent class constructor, but the subclass constructor can call the parent class constructor to initialize the code
Similar to one constructor calling another constructor:

public class Box {String name; String color; String Weight;public Box (string name,string color) {this.name=name;this.color=color;} Public Box (String name,string color,string weight) {This (name, color);//Use this to invoke another constructor without having to re-write it, which is good for the project this.weight= Weight;}}

Similar to a constructor called by another constructor using this to do the keyword, calling the parent class in the subclass using super as the critical call

Public Mybox (String name,string color,string weight,string height) {Super (name, color,weight);//Use this to invoke another constructor, There is no need to re-write, for the project is good this.height=height;}

  

Any object that is created is always executed from the top-level constructor of the class's inheritance tree, and then down to the constructor of this class

Java Review of Inheritance II

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.